Python Forum
trying to get my random number guessing game to work! NEED HELP - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: trying to get my random number guessing game to work! NEED HELP (/thread-186.html)



trying to get my random number guessing game to work! NEED HELP - RaZrInSaNiTy - Sep-28-2016

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


RE: trying to get my random number guessing game to work! NEED HELP - metulburr - Sep-28-2016

In the future please post your code in code tags, as well as post school related content in the homework forum.


RE: trying to get my random number guessing game to work! NEED HELP - nilamo - Sep-28-2016

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).


RE: trying to get my random number guessing game to work! NEED HELP - Kebap - Sep-29-2016

(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:



RE: trying to get my random number guessing game to work! NEED HELP - tinabina22 - Oct-06-2016

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))