[Python] Don't understand the error
First a bit of my code:
def new_game():
num = 100
secret_number = random.randint(0,100)
remaining_turns = math.ceil(math.log(num+1,2))
def input(guess):
global remaining_turns, secret_number
remaining_turns = remaining_turns - 1
NameError: name 'remaining_turns' is not defined
What am I doing wrong here? I thought I defined it on line 4, yet it says its undefined on line 8. Am I misunderstanding how to use global and local variables?
Posted Mar 5, 2015, 3:48 AM UTC
12
Yes. Your
new_game()function creates a local variable, and yourinput()function tries to access a global variable whichnew_game()didn't create. It isn't enough to sayglobalin one function in the program, that doesn't make the same name global in all other functions.Oh I see, thanks, fixed it!