0

Why doesn't this work, How to fix?

I'm trying to run this: times = 0 def tries(times): times = int(times) + 1 def main(): tries print(str (times) + "tries." main () I am trying to make a simple counter to increase by one every run through. However I get an error that states "times" is not defined but to my knowledge it is. Can someone explain how I am wrong and how to fix my mistakes?

10th Dec 2017, 8:39 AM
K. Grether
K. Grether - avatar
5 Antworten
+ 6
times = 0 def tries(): global times times += 1 def main(): tries() print(str (times) + " tries.") main()
10th Dec 2017, 9:06 AM
Ipang
+ 4
global allows for variables declared outside the def to also be recognized inside the def, without global, you would only make the change to times internally, the new value of times only matters inside the def, the times variable outside the def will not be altered. Hth, cmiiw
10th Dec 2017, 9:19 AM
Ipang
+ 3
You're welcome, glad to help : )
10th Dec 2017, 9:28 AM
Ipang
+ 1
Thank you for the answer, But could you explain what global does?
10th Dec 2017, 9:14 AM
K. Grether
K. Grether - avatar
+ 1
Thank you.
10th Dec 2017, 9:22 AM
K. Grether
K. Grether - avatar