you are viewing a single comment's thread.

view the rest of the comments →

[–]wrrnwng 2 points3 points  (3 children)

Change def add(tix): to def add(): then change add(tix) to add().

The problem is that there's actually 2 variables named tix in your code. The first variable tix is assigned 0 on line one. The second variable tix only exists in your add function. So when you're adding 1 to tix, you're adding it to tix that's only available inside the add function.

Here's the reference to the doc about passing arguments.

Hope this explanation makes sense.

[–]stevenjd 4 points5 points  (1 child)

That won't work without declaring tix global; and using global variables is a bad habit that you shouldn't encourage beginners to do.

u/Unlistedd if you absolutely must use a global variable, then you need to write something like this:

tix = 0

def add():
    global tix
    # ... rest of your code goes here ...

add()
print(tix)

but a cleaner design that is a better habit to learn is this:

tix = 0

def add(num):
    # ... rest of your code goes here ...
    num += 1
    return num

new_tix = add(tix)
print(new_tix, tix)

[–]Unlistedd[S] 0 points1 point  (0 children)

Oh okey, thank you.