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.')
Your indentation is not correct. Notice that the try
block and everything underneath it is no longer in your for
loop.