变量未记录值变化

时间:2018-12-31 13:57:11

标签: python python-3.x variables

我想用Python制作一个简单的Rock,Paper和Scissor游戏。它在游戏中进展顺利,但最终得分始终显示为0。

我想显示代码的较小部分,但是,我不知道问题出在哪里,所以对代码的长度感到抱歉。

我仍然是初学者,所以如果问题太傻或代码格式不正确,请原谅我。

#Rock-Paper-Scissor Game
import random


print("Please enter your name:")
userName = input()
print("Welcome " + userName)
print("The following are the rules of the game:")
print("Press 'R' for Rock")
print("Press 'P' for Paper")
print("Press 'S' for Scissor")
print("This will be a 10 point match")

userTally = 0
compTally = 0

def gameProcess(userTally, compTally): #The process of the game. It increments or decrements the value depending on the result
    print("Your turn:")
    userInput = input()
    computerChoice = random.choice(["R","P","S"])

    if userInput == "R": #User Inputs R for Rock

        if computerChoice == "R":
            print("The computer chose Rock")
            print("It's a Tie")
        elif computerChoice == "P":
            print("The computer chose Paper")
            print("Computer Won")
            compTally = compTally + 1
        elif computerChoice == "S":
            print("The computer chose Scissor")
            print("You Won")
            userTally = userTally + 1

    elif userInput == "P": #User Inputs P for Paper

        if computerChoice == "R":
            print("The computer chose Rock")
            print("You Won")
            userTally = userTally + 1
        elif computerChoice == "P":
            print("The computer chose Paper")
            print("It's a Tie")
        elif computerChoice == "S":
            print("The computer chose Scissor")
            print("Computer Won")
            compTally = compTally + 1

    elif userInput == "S": #User Inputs S for Scissor

        if computerChoice == "R":
            print("The computer chose Rock")
            print("Computer Won")
            compTally = compTally + 1
        elif computerChoice == "P":
            print("The computer chose Paper")
            print("You Won")
            userTally = userTally + 1
        elif computerChoice == "S":
            print("The computer chose Scissor")
            print("It's a Tie")
        return(userTally,compTally)

def tryCount(): #The number of tries....
    tryNum = 1
    while tryNum < 11:

        gameProcess(0, 0)
        tryNum = tryNum + 1

tryCount()


print("You scored " + str(userTally))
print("The computer scored " + str(compTally))
if userTally > compTally:
    print("CONGRATULATIONS, YOU WON.")
elif userTally < compTally:
    print("Sorry, better luck next time.")
close = input()
if close == "Random Input.":
    exit()
else:
    exit()

1 个答案:

答案 0 :(得分:2)

您将0、0传递给 gameProcess ,您将其视为函数中的分数,然后将其返回修改后的值,但实际上并没有在返回值的唯一位置使用返回值< strong> gameProcess (在 tryCount 中),因此全局userTally,compTally变量保持不变。

这是您应更改 tryCount 的方法:

def tryCount(): #The number of tries....
    global userTally, compTally
    tryNum = 1
    while tryNum < 11:

        userTally,compTally=gameProcess(userTally,compTally)
        tryNum = tryNum + 1