I'm going through the Automate the Boring Stuff with Python videos on youtube. I finished the one about random number guessing game and am now adding some stuff to it to make it more complete.
I added some code that makes it so that if you don't enter an integer (which creates a ValueError) the program tells you that you didn't enter an integer. However, the point of the game is that you only get 6 guesses, but when the error comes up it, it resets the number of guesses.
How can I make it so that the error doesn't reset the number of cases?
Here's the code:
# This is a guess the number game.
import random
print ('Hello, what is your name?')
name = input ()
name = name.strip()
print ('Well, ' name ' I am thinking of a number between 1 and 20.')
secretNumber = random.randint (1, 20)
print ('DEBUG: Secret number is ' str(secretNumber))
while True:
try:
for guessesTaken in range (1, 7):
print ('Take a guess.')
guess = int (input ())
if guess < secretNumber:
print ('Your guess is too low.')
elif guess > secretNumber:
print ('Your guess is too high.')
else:
break # This condition is for the correct guess
if (guess == secretNumber and guessesTaken == 1):
print ('Good job, ' name '! You guessed my number in ' str(guessesTaken) ' guess.')
break
elif (guess == secretNumber and guessesTaken > 1):
print ('Good job, ' name '! You guessed my number in ' str(guessesTaken) ' guesses.')
break
else:
print ('Nope. The number I was thinkig of was ' str(secretNumber))
break
except ValueError:
print ('You did not enter a number.') # This condition is for if a non-integer is entered.
I feel like I've tried every combination of where I can but the try and for loop, but can't seem to figure it out.
CodePudding user response:
The outer while loop seems unnecessary because it allows you to try even after 6 attempts.
Therefore the break in the last three conditions will also be deleted.
You should put the try into the for loop so that if an exception is raised, the program will continue to count the number of attempts.
I would do something like:
# This is a guess the number game.
import random
print('Hello, what is your name?')
name = input()
name = name.strip()
print('Well, ' name ' I am thinking of a number between 1 and 20.')
secretNumber = random.randint(1, 20)
print('DEBUG: Secret number is ' str(secretNumber))
for guessesTaken in range(1, 7):
try:
print('Take a guess.')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low.')
elif guess > secretNumber:
print('Your guess is too high.')
else:
break # This condition is for the correct guess
except ValueError:
print('You did not enter a number.') # This condition is for if a non-integer is entered.
if (guess == secretNumber and guessesTaken == 1):
print('Good job, ' name '! You guessed my number in ' str(guessesTaken) ' guess.')
elif (guess == secretNumber and guessesTaken > 1):
print('Good job, ' name '! You guessed my number in ' str(guessesTaken) ' guesses.')
else:
print('Nope. The number I was thinkig of was ' str(secretNumber))
The except block also should be moved, to be related to the try block, under the block of the loop because we don't want to stop the for loop as I mentioned before.
CodePudding user response:
This is a guess the number game.
import random
print ('Hello, what is your name?') name = input () guess = 0
name = name.strip()
print ('Well, ' name ' I am thinking of a number between 1 and 20.') secretNumber = random.randint (1, 20)
print ('DEBUG: Secret number is ' str(secretNumber))
while True: for guessesTaken in range (1, 7): try: print ('Take a guess.') guess = int (input ())
if guess < secretNumber:
print ('Your guess is too low.')
elif guess > secretNumber:
print ('Your guess is too high.')
else:
break # This condition is for the correct guess
except ValueError:
print ('You did not enter a number.') # This condition is for if a non-integer is entered.
if (guess == secretNumber and guessesTaken == 1):
print ('Good job, ' name '! You guessed my number in ' str(guessesTaken) ' guess.')
break
elif (guess == secretNumber and guessesTaken > 1):
print ('Good job, ' name '! You guessed my number in ' str(guessesTaken) ' guesses.')
break
else:
print ('Nope. The number I was thinkig of was ' str(secretNumber))
break
