Why it's not error message | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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

12th May 2019, 1:34 PM
Ahmed Mohamed
Ahmed Mohamed - avatar
7 ответов
+ 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!
12th May 2019, 1:37 PM
Théophile
Théophile - avatar
0
Note that the following code will throw an error. def foo(): total+=1 return total total=0 print(foo())
12th May 2019, 2:38 PM
Diego
Diego - avatar
0
Because total isn't a global variable. def foo() : global total total+=1 return total No error but value of total changes
12th May 2019, 2:41 PM
Théophile
Théophile - avatar
0
Thèophile I don't understand what do you mean?
12th May 2019, 2:44 PM
Ahmed Mohamed
Ahmed Mohamed - avatar
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!
12th May 2019, 2:48 PM
Théophile
Théophile - avatar
12th May 2019, 2:54 PM
Théophile
Théophile - avatar
0
I got it thanks for all of you 💛
13th May 2019, 2:52 AM
Ahmed Mohamed
Ahmed Mohamed - avatar