gpt4 book ai didi

python - 如何在Python中向用户定义函数添加计数器?

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:08 25 4
gpt4 key购买 nike

>>> Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Maximillian\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Python-koding\steinsakspapir.py", line 64, in scissors
botwin += 1
UnboundLocalError: local variable 'botwin' referenced before assignment

-尝试从代码的其余部分中删除 botwin 和 youwin-尝试使用youwin = youwin + 1

    def scissors():
bot = random.randint(1,3)
user = 3
if user == bot:
printfuver = ("Stalemate, booth players choose scissors")
printfu = Label(lowerframe, text=printfuver, fg="blue")
printfu.pack()
if user == 3 and bot == 1:
printfuver = ("Rock crushes scissors, bot win! ")
printfu = Label(lowerframe, text=printfuver, fg="red")
printfu.pack()
botwin += 1
if user == 3 and bot == 2:
printfuver = ("Scissors cut paper, you win! ")
printfu = Label(lowerframe, text=printfuver, fg="green")
printfu.pack()
youwin += 1

只是希望每次运行函数后 botwin 的值增加 1。

提前致谢

最佳答案

除了代码中的缩进错误之外,这可能是一个范围问题。检查并确保您的代码中某处有 botwin = 0 或类似的内容。如果该代码位于函数中或超出全局范围,请将其放在代码的顶部。然后,在引用它的所有函数中,将 global botwin 放在函数的开头,如所述 here .

希望这对您有帮助。

关于python - 如何在Python中向用户定义函数添加计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53979223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com