+ 2
Why it's not error message
The (total) variable has been defined after function definition but the code worked normally https://code.sololearn.com/czvByhfmh05c/?ref=app
7 Respostas
+ 4
The function is called after the definition of variable total, so there is no issue here! Python cares if a variable exists only when the function is called, not before!
0
Note that the following code will throw an error.
def foo():
total+=1
return total
total=0
print(foo())
0
Because total isn't a global variable.
def foo() :
global total
total+=1
return total
No error but value of total changes
0
Thèophile I don't understand what do you mean?
0
By global variable I mean a variable that can be modified everywhere in a program. Here your variable total isn't global variable.
I only answered back to Diego...
Like Diego said, his code will produce an error because total isn't a global variable.
I don't know if it's clear... I explain so bad, sorry!
0
I got it thanks for all of you 💛