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?
5 Answers
+ 6
times = 0
def tries():
global times
times += 1
def main():
tries()
print(str (times) + " tries.")
main()
+ 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
+ 3
You're welcome, glad to help : )
+ 1
Thank you for the answer, But could you explain what global does?
+ 1
Thank you.