-1

I'm trying a small program out of the Automate the Boring Stuff book.

I want to ensure that someone inputting a non-int variable will not crash the program. The code kicks out an error saying that the "break" command is not in the loop. Any help would be appreciated.

import random
print ('Hello. What is your name?')
name = input()
secretNumber=random.randint(1,20)
print('Well ' + name + ', I\'m thinking of a number between 1 and 20.')

for guessesTaken in range(1,7):
    print()
    print ('Take a guess')
try:
    guess = int(input())

except ValueError:
    print ('I said guess a number not letters or words.')

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

if guess == secretNumber:
    print('Good job! You guessed it.')
else:
    print('Nope the number I was thinking of was ' + str(secretNumber)+' sorry.')
Querenker
  • 2,242
  • 1
  • 18
  • 29
Michael Porter
  • 229
  • 3
  • 12

2 Answers2

2

Your indentation is not correct. Notice that the try block and everything underneath it is no longer in your for loop.

bcongdon
  • 137
  • 3
  • 9
1

Well, first of all, remember to always post your code in here, thay way you will avoid the downvotes.

At looking at your code i could easily tell you a few things:

1) Indent this piece of code since it is actually out of the for loop.

try:
    guess = int(input())

except (ValueError, NameError):
    print ('I said guess a number not letters or words.')
    break

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

2) You should make a break in the except block (like i did above) so it won't execute the next block of if statements or you will also get the exception for NameError because since the guess variable wasn't created the next if statement if guess "<" secretNumber: would throw an exception.

3) This code is kinda broken because if it throws an exception and guess isn't created, the last if statement out of the for loop would also throw an exception, so i recommend you to initialize that variable before the for statment like this: guess = 0.

Your final code should look like this:

import random
print ('Hello. What is your name?')
name = input()
secretNumber=random.randint(1,20)
print('Well ' + name + ', I\'m thinking of a number between 1 and 20.')
guess = 0

for guessesTaken in range(1,7):
    print()
    print ('Take a guess')
    try:
        guess = int(input())

    except (ValueError, NameError):
        print ('I said guess a number not letters or words.')
        break

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

if guess == secretNumber:
    print('Good job! You guessed it.')
elif guess == 0:
    None
else:
    print('Nope the number I was thinking of was ' + str(secretNumber)+' sorry.')

There is still room for improvement but this will fulfill your request.

Have a good day =).

Leo
  • 472
  • 5
  • 9