Home > Mobile >  Can I define an integer in a loop only once?
Can I define an integer in a loop only once?

Time:04-21

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. 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

CodePudding user response:

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)

CodePudding user response:

You can make count a parameter of the function, that way you can initiate it and change its value outside of the function:

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

    if value in plusOne:
        count  = count   1
        print(count)
  • Related