0

I'm working through the Automate the Boring Stuff with Python Course on YouTube/udemy, and I'm on a lesson that covers a simple number guessing game. LINK

The code itself I more or less understand:

# This is a guess the number game.
import random

print('Hello. What is your name?')
name = input ()

print('Well, ' + name + ', I am thinking of a number between one and a ONE HUNDRED.')
secretNumber = random.randint(1, 100)

for guessesTaken in range(1, 7):
    print('Take a guess.')

    if guess < secretNumber: 
        print('Your guess is too low.')
    elif guess > secretNumber:
        print('Your guess is too high.')

    else:
        break # This condition is for correct guesses!

if guess == secretNumber:
    print('Good job, ' + name + ', You took ' + str(guessesTaken) + ' guesses.')
else:
    print('Nope. The number I was thinking of was ' + str(secretNumber) + '. You Failed!')

You take a random integer between 1 and 100. The user tries to guess it. If they get it right, it tells you. If you're unable within a certain number of guesses, you lose.

I wanted to add a 'try' 'except' statement to deal with a non-integer input that the user might make. It took me a while to get around the syntax errors, but I managed to write:

for guessesTaken in range(1, 7):
    print('Take a guess.')
    try: 
        guess = int(input())
    except ValueError:
        print ("That's not an integer!")
    if guess < secretNumber: 
        print('Your guess is too low.')
    elif guess > secretNumber:
        print('Your guess is too high.')

    else:
        break # This condition is for correct guesses!

This produces the correct error message. However, it produces another 'NameError' because guess is now not properly defined.

How should I amend the flow statements to get me back to the point where the program allows me to input a different value?

  • You can use **`while True`** to wrap `try except` and use **`break`** at the end of the try block – alexzshl May 12 '20 at 02:39
  • You seem to have posted more code than what would be reasonable for your issue. Please read [ask] and how to make a [mre]; providing a MRE helps users answer your question and future users relate to your issue. – rizerphe May 12 '20 at 03:28

2 Answers2

1

The problem is that when you catch the exception in your except statement, execution continues into the following if expression, at which point you get your NameError because guess isn't defined (because the assign statement raised a ValueError exception).

If the user enters an invalid input, you problem just want them to try again. That's what the continue statement is for:

for guessesTaken in range(1, 7):
    print('Take a guess.')
    try: 
        guess = int(input())
    except ValueError:
        print ("That's not an integer!")
        continue
larsks
  • 277,717
  • 41
  • 399
  • 399
0

Your problem is that the loop is continuing after printing "That's not an integer". Use a continue statement to solve this problem.

for guessesTaken in range(1, 7):
print('Take a guess.')
try: 
    guess = int(input())
except ValueError:
    print ("That's not an integer!")
    i -= 1
    continue // Returns to start of loop without executing the rest of it
if guess < secretNumber: 
    print('Your guess is too low.')
elif guess > secretNumber:
    print('Your guess is too high.')

I've also added i -= 1 to reduce i by one. This is so that the user's number of guess left does not change in case they accidentally type a letter.

tersrth
  • 861
  • 6
  • 18