首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的石头布剪刀游戏,返回分数的问题?

简单的石头布剪刀游戏,返回分数的问题?
EN

Stack Overflow用户
提问于 2012-06-22 13:44:55
回答 1查看 597关注 0票数 0

我正在为我的编程课写这个石布剪刀程序,在程序的最后显示满分时遇到了一些问题。我是Python的超级初学者,所以这里没什么特别的。由于某些原因,当我运行程序时,无论游戏循环多少次,显示的唯一分数都是1。我在这里做错了什么?

代码语言:javascript
复制
from myro import *
from random import *

def announceGame():
    """ Announces the game to the user """ 
    speak("Welcome to Rock, Paper, Scissors. I look forward to playing you.") 

def computerMove():
    """ Determines a random choice for the computer """ 
randomNumber = random()
    if randomNumber == 1:
       compMove = "R"
    elif randomNumber == 2: 
       compMove = "P"
    else:
       compMove = "S"
return compMove 

def userMove():
    """ Asks the user to input their choice.""" 
    userChoice = raw_input("Please enter R, P, or S: ")
    return userChoice

def playGame(userChoice, compMove):
    """ Compares the user's choice to the computer's choice, and decides who wins.""" 

    global userWin
    global compWin
    global tie

    userWin = 0
    compWin = 0
    tie = 0

    if (userChoice == "R" and compMove == "S"):
       userWin = userWin + 1
       print "You win."

    elif (userChoice == "R" and compMove == "P"):
       compWin = compWin + 1
       print "I win."

    elif (userChoice == "S" and compMove == "R"):
       compWin = compWin + 1
       print "I win."

    elif (userChoice == "S" and compMove == "P"):
       userWin = userWin + 1
       print "You win"

    elif (userChoice == "P" and compMove == "S"):
       compWin = compWin + 1
       print "I win"

    elif (userChoice == "P" and compMove == "R"):
       userWin = userWin + 1
       print "You win"

    else:
       tie = tie + 1
       print "It's a tie"


    return compWin, userWin, tie


def printResults(compWin, userWin, tie):
    """ Prints the results at the end of the game. """
    print "     Rock Paper Scissors Results "
    print "------------------------------------" 
    print "Computer Wins: " + str(compWin)
    print "User Wins: " + str(userWin)
    print "Ties: " + str(tie) 

def main():
    announceGame()
    for game in range(1,6):
       u = userMove()
       c = computerMove()
       game = playGame(u,c)

    printResults(compWin, userWin, tie)  


main() 
EN

回答 1

Stack Overflow用户

发布于 2012-06-22 13:48:42

playGame中,将userWincompWintie设置为零。因此,每次调用该函数时,在添加新值之前,它们都会被设置为零。您应该在循环中调用的函数外部初始化这些变量。(例如,您可以在announceGame中初始化它们。)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11150756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档