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

2024-05-13 12:39:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的编程课写一个石头-布-剪刀程序,在程序结束时得到满分有一些问题。我是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() 

Tags: andtheto程序gamedefwinprint
1条回答
网友
1楼 · 发布于 2024-05-13 12:39:10

playGame中,将userWincompWin、和{}设置为零。所以每次你调用这个函数,它们都会在新值加入之前被设置为零。应该在循环中调用的函数外部初始化这些变量。(例如,可以在announceGame中初始化它们。)

相关问题 更多 >