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:
And here is the one what isn't working:
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
Here is the orignal:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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()) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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()) |
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