簡體   English   中英

如何在Python中向用戶定義函數添加計數器?

[英]How to add counters to User-Defined Functions in Python?

>>> 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在你的函數的開頭解釋這里

我希望這可以幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM