Python 尝试在T或F游戏中使用全局变量
我不知道如何修正这个错误,这样我就可以为每一场获胜的比赛加上分数,并进行客场比赛 你选择你想玩多少轮,当你玩每一轮时,1会从“轮数”变量中去掉,直到你有0为止,这将不再让你玩了,直接带你到分数函数 积分用于跟踪您/机器人赢得的游戏,以查看最终谁赢了(所有回合完成后) 我得到以下错误:Python 尝试在T或F游戏中使用全局变量,python,global,Python,Global,我不知道如何修正这个错误,这样我就可以为每一场获胜的比赛加上分数,并进行客场比赛 你选择你想玩多少轮,当你玩每一轮时,1会从“轮数”变量中去掉,直到你有0为止,这将不再让你玩了,直接带你到分数函数 积分用于跟踪您/机器人赢得的游戏,以查看最终谁赢了(所有回合完成后) 我得到以下错误: Traceback (most recent call last): File "python", line 99, in <module> File "python", line 13, in
Traceback (most recent call last):
File "python", line 99, in <module>
File "python", line 13, in login
File "python", line 30, in start
File "python", line 33, in rcheck
UnboundLocalError: local variable 'round' referenced before assignment
回溯(最近一次呼叫最后一次):
文件“python”,第99行,在
登录中第13行的文件“python”
文件“python”,第30行,开头
rcheck中的文件“python”,第33行
UnboundLocalError:赋值前引用的局部变量“round”
这是我的密码:
from time import sleep
import random
print("Rock, Paper, Scissors. By Leona Bryant")
print("")
def login():
username=input("Username: ")
password=input("Password: ")
if (username == "shinleona" ) and (password == "hacker101"):
print ("")
print ("Welcome back Leona!")
start()
else:
print("")
print ("Invalid credidentals, try again.")
login()
def start():
print("")
round=int(input("How many rounds would you like to play? Please pick from 3-10"))
if (round <3 ):
print("Invalid number, try again.")
start()
else:
print("")
print("Loading..")
sleep(3)
rcheck()
def rcheck():
if (round <=0 ):
print("End of game!")
print("Calculating scores...")
score()
else:
round-=1
game()
def game():
print("")
rps=("rock" , "paper" , "scissors")
bot = random.choice(rps)
user=input("Rock..Paper..Scissors..SHOOT!").lower()
print("The bot chose" , bot )
if (bot == user):
print ("This round was a draw, you both gain 1 point!")
point+=1
bpoint+=1
rcheck()
elif ( (bot == "paper") and (user == "rock") ) or ( (bot == "scissors") and (user == "paper") ) or ( (bot == "rock") and (user == "scissors") ):
print ("You Lose this round, Bot gains 1 point!")
bpoint+=1
rcheck()
else:
print("You won this round, User gains 1 point!")
point+=1
rcheck()
def score():
if (point > bpoint):
print("Congratulations, you won the game with" , point , "points")
again()
else:
print("Sorry, you lost the game. The bot won with" , bpoint , "points")
again()
def again():
play=input("Play again?").lower
if (play[0] == "y"):
start()
elif (play[0] == "n"):
print("Thanks for playing!")
exit()
else:
print ("Invalid answer, try again.")
again()
round = 0
point = 0
bpoint = 0
global round
global bpoint
global point
login()
从时间导入睡眠
随机输入
印刷品(“石头、布、剪刀,由利昂娜·布莱恩特创作”)
打印(“”)
def login():
用户名=输入(“用户名:”)
密码=输入(“密码:”)
如果(用户名==“shinleona”)和(密码==“hacker101”):
打印(“”)
打印(“欢迎回来Leona!”)
开始()
其他:
打印(“”)
打印(“无效的凭据,请重试。”)
登录()
def start():
打印(“”)
round=int(输入(“您想玩多少回合?请从3-10中选择”))
如果在函数中使用(roundglobal
表示它正在修改的值来自外部(模块)范围,则在模块级代码中无效。您应该删除这些行
global round
global bpoint
global point
在MATLAB中,它们将使名称全局可用。在Python中,它们什么也不做,因为变量已经在模块范围中定义
将行全局舍入
添加到rcheck
(并将相应行添加到使用全局变量的函数)。这告诉python函数中的名称round
实际上是一个全局变量,即在模块中定义的。如果没有global
语句,函数将尝试执行round=round+1
,但右侧的round
未在函数的作用域中定义
当然,如果可以避免的话,不要使用全局变量。编写一个具有所有必要状态和功能的类,或者至少将值传递给函数并通过返回值接受更新。您没有将全局变量引入到方法中。请在rcheck()方法的顶部添加以下内容:
这应该允许您使用此全局变量。变量分配(包括函数分配)将全局定义移到顶端,它应该工作。这个程序也在请求使用,也许考虑重构,以便<代码>圆,<代码>点和<代码> Boo.<代码>是类属性而不是全局值。不要使用全局变量。函数参数和返回值是为THI创建的。s、 除非绝对必要,否则全局变量永远不应该是你想要的。在这种情况下,我看不出使用它们有什么好处。显式地将参数传递给函数,并显式地从函数返回值。一旦你编写的程序不仅仅是小测试,全局可变变量就会开始导致各种问题。如果你ant为了避免传递和返回变量,至少将状态存储在封闭的命名空间(类/对象)中。
global round