+ 6

I got question in define a function in Python

count=0 def mine(): while count<10 count+=1 print(count) mine() output is Unbound Local Error which means count must be inside function(def) so all variables should inside def Am I correct??? Also I'm troubling in def for long time if anyone can explain about function shortly!

6th Dec 2017, 1:59 PM
Shadow Windster
Shadow Windster - avatar
2 odpowiedzi
+ 6
Yes, you are right. The variable `count` can be accessed by functions, but not modified by them unless you use the `global` keyword like this: ``` count = 0 def mine(): global count while count < 10: count += 1 print(count) mine() ```
6th Dec 2017, 6:53 PM
Supersebi3
Supersebi3 - avatar
+ 6
yes the variable must be inside the function
20th Dec 2017, 4:55 PM
Shadow Windster
Shadow Windster - avatar