简体   繁体   English

我可以在一个循环中只定义一次 integer 吗?

[英]Can I define an integer in a loop only once?

This is a piece of my code, I run the callback every time I press the ok button on my pop-up so I only want count to be defined once.这是我的一段代码,每次按下弹出窗口上的确定按钮时我都会运行回调,所以我只想定义一次计数。 After that I need to exclude the line "count = 0" from being looped.之后,我需要将“count = 0”行排除在循环之外。 When I put it outside of the loop, even using global, it gives me an error saying it doesn't know what count is.当我把它放在循环之外时,即使使用全局,它也会给我一个错误,说它不知道计数是什么。 Any way I could fix this?有什么办法可以解决这个问题吗? (Here is my code) (这是我的代码)

def callback():
    count = 0
    value = int(entry_field.get())
    entry_field.delete("0", tk.END)


    if value in plusOne:
        count += count + 1
        print(count)

Thanks谢谢

define it as global?:将其定义为全局?:

count = 0
def callback():
    global count
    value = int(entry_field.get())
    entry_field.delete("0", tk.END)


    if value in plusOne:
        count += count + 1
        print(count)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM