-3

I have an assignment to write a number-guessing game, with these requirements:

  1. End the game when the guess is correct
  2. Tell if the guess number is smaller or larger when the guess is wrong
  3. Keep running until there are 5 failed attempts

I wrote this code that uses a for loop:

number = 69

for x in range (5):
  guess = float(input('Please enter a number:'))
  if guess == number:
    print('Congratulations')
    break
  elif guess < number:
    print('Number too small')   
  else:
    print('Number too large')

for x in range(5):
  if guess != number:
    print('Game Over')
  break

How can I modify the code to use a while loop instead? I got this far:

number = 69 
guess=float(input('Please enter a number: ')) 

while True:
  if guess == number:
    break
  guess=float(input('Please enter a number: '))

print('Congratulations')

but this does not verify the guess or check the number of attempts.

When I tried to add the rest of the logic, like so:


number = 69 
guess=float(input('Please enter a number: ')) 

def guess_game():
  while True:
    if guess == number:
      break
    guess=float(input('Please enter a number: '))

if guess < number:
  print('Number too small')
else:
  print('Number too large')

x = 0
while x < 6:
  guess_game()
  if guess != number:
    print('Game Over')
    break

print('Congratulations')

I got this error: 'UnboundLocalError: local variable 'guess' referenced before assignment'

What is wrong, and how do I fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Byt
  • 1
  • 2
  • 2
    Welcome to Stack Overflow. Thanks for sharing your code. Can you share the error message, too? – nofinator Oct 03 '22 at 16:31
  • Welcome to Stack Overflow. Please read [ask] and take the [tour]. Note well that this is **not a discussion forum**; we're looking for *one, specific, directly asked* question in a post, which should be what is left over [after your best effort](https://meta.stackoverflow.com/questions/261592) to fix the problem yourself, and which hasn't been asked before. We aren't interested in anything about you (including your skill level), we aren't here to make conversation, and we don't offer general advice on the code - we answer a question. – Karl Knechtel Oct 03 '22 at 17:05
  • I [edit]ed the question to show better question-asking style. As far as the error message goes, please read https://stackoverflow.com/questions/370357; this is an extremely common question. In terms of fixing the code, you are overcomplicating it; if you only want to replace the loop, then it isn't necessary to change multiple other things about the logic at the same time. Try to approach changes to code in a systematic way, and change only a little bit at a time. Instead of rewriting code in order to use a new technique (like a `while` loop instead of a `for` loop, try to get there directly. – Karl Knechtel Oct 03 '22 at 17:12

3 Answers3

-1

Your loop will be endless if you don't specify the fail scenario (in your case if x = 5) so you need to add that too. Also, don't forget to increment the x variable!

number = 69 
guess = float(input('Please enter a number: ')) 
x = 0

while True:
    if x == 4:
        print('Game over!')
        break
    if guess == number:
        print('Congratulations!')
        break
    guess = float(input('Please enter a number: '))
    x += 1
  • No problem! You can also add 'if x > number' and 'if x < number' to print out if the number is too small or too big. – CodingDaveS Oct 03 '22 at 16:43
-1

You made some good efforts.

This works:

number = 69


while guess != number:
    guess = float(input('Please enter a number:'))
    if guess < number:
        print('too low')
    elif guess > number:
        print('too high')
    else:
        print('you got it')

print('game over')
D.L
  • 4,339
  • 5
  • 22
  • 45
-1

I just used an incrementation variable instead, try this:

number = 69
i = 0

while i < 5:
    guess = float(input('Please enter a number:'))
    if guess == number:
        print('Congratulations')
        break
    elif guess < number:
        print('Number too small')   
    else:
        print('Number too large')   
    i+=1
    
if guess != number:
    print('Game over')
Sami Walid
  • 24
  • 4