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に設定します。したがって、その関数を呼び出すたびに、新しい値が追加される前にゼロに設定されます。これらの変数は、ループ内で呼び出している関数の外で初期化する必要があります。(たとえば、 で初期化できます。)userWincompWintieannounceGame

于 2012-06-22T05:48:42.293 に答える