为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Smart猫小萌 2021-09-28 21:07:20
>>> Exception in Tkinter callbackTraceback (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 += 1UnboundLocalError: 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 的值。
查看完整描述

1 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

除了代码中的缩进错误之外,这可能是一个范围问题。检查以确保您botwin = 0在代码中的某处拥有或类似的东西。如果该代码在函数中或在全局范围之外,请将其放在代码的顶部。然后,在所有引用它global botwin的函数中,按照此处的说明放在函数的开头。

我希望这可以帮助你。


查看完整回答
反对 回复 2021-09-28
  • 1 回答
  • 0 关注
  • 263 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号