Hi, I have to create a random number guessing game for school and i am trying to add attempts but so far eveything i have done ends in a error. i have a copy of the orignal code i made and the code what i am trying to get working. Any help will be highly appriecated.
Here is the orignal:
What number from 1 - 10
5
Unlucky, You got it wrong
Traceback (most recent call last):
File "C:\Users\Chris\Downloads\Random 2 (4).py", line 23, in <module>
print(menu())
File "C:\Users\Chris\Downloads\Random 2 (4).py", line 16, in menu
attempts = attempts + 1
UnboundLocalError: local variable 'attempts' referenced before assignment
Here is the orignal:
from random import randint import sys def menu(): print("What number from 1 - 10 ") guess = int(input()) if guess == number: print("You got it right ") print("It was", number) elif guess != number: print("Unlucky, You got it wrong") print(menu()) number = (randint(1,10)) print(menu())And here is the one what isn't working:
from random import randint import sys attempts = 0 if attempts == 3: print("GoodBye You got no attempts left") def menu(): print("What number from 1 - 10 ") guess = int(input()) if guess == number: print("You got it right ") print("It was", number) elif guess != number: print("Unlucky, You got it wrong") attempts = attempts + 1 print ("You have", attempts," attempts left") print(menu()) number = (randint(1,10)) if attempts == 3: print("GoodBye You got no attempts left") print(menu())the error is:
What number from 1 - 10
5
Unlucky, You got it wrong
Traceback (most recent call last):
File "C:\Users\Chris\Downloads\Random 2 (4).py", line 23, in <module>
print(menu())
File "C:\Users\Chris\Downloads\Random 2 (4).py", line 16, in menu
attempts = attempts + 1
UnboundLocalError: local variable 'attempts' referenced before assignment