Python Forum
trying to get my random number guessing game to work! NEED HELP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to get my random number guessing game to work! NEED HELP
#1
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:


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
Reply
#2
In the future please post your code in code tags, as well as post school related content in the homework forum.
Recommended Tutorials:
Reply
#3
You're referencing a variable that's never defined within your function. ...and then you're recursively calling that function. Instead of doing either, consider using a while loop (or something like "for attempt in range(3):" with a return/break if they get it right).
Reply
#4
(Sep-28-2016, 04:14 PM)nilamo Wrote: you're recursively calling that function

Which means, inside the block "def menu()" you call menu(). That is not needed here, in fact may create problems.

Also these lines at the beginning, would you expect this if-block to ever be executed?

attempts = 0
if attempts == 3:
Reply
#5
Give this a try:
from random import randint
randomNum = randint(1,100)
print('I am thinking of a number between 1 and 100.')
 
for guessesTaken in range(1, 100):
    print('Take a guess.')
    guess = int(input())
 
    if guess < randomNum:
        print('Your guess is too low.')
    elif guess > randomNum:
        print('Your guess is too high.')
    else:
        break    
if guess == randomNum:
    print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
    print('Nope. The number I was thinking of was ' + str(randomNum))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Play again, in a guessing game banidjamali 4 11,659 Jan-22-2021, 06:23 AM
Last Post: banidjamali
  Help - random number/letter guessing game juin22 1 3,189 Aug-16-2020, 06:36 AM
Last Post: DPaul
  Dynamically chosing number of players in a "game" nsadams87xx 5 4,134 Jul-17-2020, 02:00 PM
Last Post: deanhystad
  Can someone help me optimize this game for large number of strings Emekadavid 13 4,910 Jul-06-2020, 06:16 PM
Last Post: deanhystad
  making a guessing number game blacklight 1 2,190 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Guessing game with 4 different digits LinseyLogi 6 3,633 Mar-29-2020, 10:49 AM
Last Post: pyzyx3qwerty
  Guess the number game jackthechampion 5 3,173 Mar-07-2020, 02:58 AM
Last Post: AKNL
  Asking for help in my code for a "Guess the number" game. Domz 5 3,809 Aug-14-2019, 12:35 PM
Last Post: perfringo
  About the random number generating shang2019 2 2,563 Jan-13-2019, 02:40 AM
Last Post: gitiya
  Guessing game with comparison operators Drone4four 9 13,752 Dec-02-2018, 06:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020