Python 全局变量不工作,请帮助解决noob

Python 全局变量不工作,请帮助解决noob,python,python-3.x,variables,global-variables,Python,Python 3.x,Variables,Global Variables,我试图帮助一位朋友完成他们的作业,但我不断地遇到一个错误 赋值前引用的局部变量“totalPoint” 我曾尝试将全局totalPoint放在函数的第一行内、函数的外部和代码的顶部。我无法使其工作,如何解决它?虽然您可以在不使用global关键字的情况下访问全局变量,但如果您要修改它们,则必须使用global关键字。例如: def cpuFireuserInput,点: 如果要修改全局变量,需要将其添加到每个局部范围 全局全点 如果userInput==0: totalPoint=totalPo

我试图帮助一位朋友完成他们的作业,但我不断地遇到一个错误

赋值前引用的局部变量“totalPoint”


我曾尝试将全局totalPoint放在函数的第一行内、函数的外部和代码的顶部。我无法使其工作,如何解决它?

虽然您可以在不使用global关键字的情况下访问全局变量,但如果您要修改它们,则必须使用global关键字。例如:

def cpuFireuserInput,点: 如果要修改全局变量,需要将其添加到每个局部范围 全局全点 如果userInput==0: totalPoint=totalPoint+int0 tieCount=tieCount+1 lossCount=lossCount+0 winCount=winCount+0 cpuChoice=火 wlt=t elif userInput==1: 总点=总点+积分*-1 tieCount=tieCount+0 lossCount=lossCount+1 winCount=winCount+0 cpuChoice=火 wlt=l elif userInput==2: 总点=总点+积分 tieCount=tieCount+0 lossCount=lossCount+0 winCount=winCount+1 cpuChoice=火 wlt=w
默认情况下,在所有函数之外声明变量将导致全局变量。但是,如果要在函数中创建全局变量,则必须使用global关键字

我解决了它,我把我的全局变量放在if和elif语句中,我应该把它放在函数def的开头,当我运行代码时,程序打印出来你有10分。进来你能指出你的程序在代码中的什么地方坏了吗?
import random


#--------------------------------------------------------------------#



tieCount = 0
lossCount = 0
winCount = 0
totalPoint = 10
i = 3



#--------------------------------------------------------------------#



def cpuFire(userInput, points):
    if userInput == 0: 
        totalPoint = totalPoint + int(0)
        tieCount = tieCount + 1
        lossCount = lossCount + 0
        winCount = winCount + 0
        cpuChoice = "fire"
        wlt = "t"
    elif userInput == 1:
        totalPoint = totalPoint + int(points * -1)
        tieCount = tieCount + 0
        lossCount = lossCount + 1
        winCount = winCount + 0   
        cpuChoice = "fire"
        wlt = "l"
    elif userInput == 2:
        totalPoint = totalPoint + int(points)
        tieCount = tieCount + 0
        lossCount = lossCount + 0
        winCount = winCount + 1   
        cpuChoice = "fire"
        wlt = "w"



def cpuGrass(userInput, points):
    if userInput == 1: 
        print("Tie! Your points remain unchanged")
        totalPoint = totalPoint + int(0)
        tieCount = tieCount + 1
        lossCount = lossCount + 0
        winCount = winCount + 0   
        cpuChoice = "grass"    
        wlt = "t"
    elif userInput == 2:
        print("You lost the game with X points deducted.")
        totalPoint = totalPoint + int(points * -1)
        tieCount = tieCount + 0
        lossCount = lossCount + 1
        winCount = winCount + 0    
        cpuChoice = "grass"  
        wlt = "l"
    elif userInput == 0:
        print("You won the game with X points added.")
        totalPoint = totalPoint + int(points)
        tieCount = tieCount + 0
        lossCount = lossCount + 0
        winCount = winCount + 1     
        cpuChoice = "grass"  
        wlt = "w"

def cpuWater(userInput, points):
    if userInput == 2:
        print("Tie! Your points remain unchanged")
        totalPoint = totalPoint + int(0)
        tieCount = tieCount + 1
        lossCount = lossCount + 0
        winCount = winCount + 0 
        cpuChoice = "water"  
        wlt = "t"
    elif userInput == 0:
        print("You lost the game with X points deducted.")
        totalPoint = totalPoint + int(points * -1)
        tieCount = tieCount + 0
        lossCount = lossCount + 1
        winCount = winCount + 0  
        cpuChoice = "water" 
        wlt = "l"
    elif userInput == 1:
        print("You won the game with X points added.")
        totalPoint = totalPoint + int(points)
        tieCount = tieCount + 0
        lossCount = lossCount + 0
        winCount = winCount + 1  
        cpuChoice = "water" 
        wlt = "w"

def cpuRand():
    cpuList = ["f","g","w"]
    rand = random.choice(cpuList)
    if rand == "f":
        cpuFire(userInput, points)
    elif rand == "g":
        cpuGrass(userInput, points)
    elif rand == "w":
        cpuWater(userInput, points)



#--------------------------------------------------------------------#



while i > 0:
    print("You have " + str(totalPoint) + " points.")
    points = int(input("Enter the number of points to be used for next game: "))
    if points > totalPoint:
        print("You do not have neough points.")
        i = i - 0
    else:
        userInput = int(input("Enter 0(fire), 1(grass) or 2(water): "))
        if userInput > 2 or userInput < 0:
            print("You entered an invalid option, you lost!")
            break
        else:
            cpuRand()
            if wlt == "t":
                print("draw!")
            elif wlt == "l":
                print("You are " + str(userInput) + " and computer is " + str(cpuChoice) + ", you lost!")
            elif wlt == "w":
                print("You are " + str(userInput) + " and computer is " + str(cpuChoice) + ", you won!")
            i = i - 1