0

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

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() 
4

1 回答 1

2

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

于 2012-06-22T05:48:42.293 回答