Reputation: 73
# 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 1 to 20.')
secretNumber = random.randint(1,20)
print('Debug:' + str(secretNumber))
for guessTaken in range (1,7):
print('Take a guess. '+ name +'!' )
guess =int(input())
if guess<secretNumber:
print('Your guess is too low')
elif guess>secretNumber:
print('Your guess is too high')
else:
break # This is for correct guess is equal.
if guess ==secretNumber:
print('Good Job, ' +name+ '! You guess my number in ' + str(guessTaken)+ ' guesses!')
else:
print('Nope, The number I was thinking of was ' +str(secretNumber))
Hi my fellow super coders,
So I am trying to put Try and Except block in this program, I try it putting just after guess=int(input()) unfortunately i am not able to make it work.
So i am trying to handle the ValueError, So lets say user needs to input Integers (Numbers), If he types string like , "One","Six" etc. the program crashes. I want to handle this case. Please can some help me out. :)
Thank You so much. Cobra
Upvotes: 2
Views: 55
Reputation: 16
If the user enters something that is not a number python will skip to the exception because an error will occur, I added some text to the prints to clarify the rules of the game. I used else after for loop which is very useful sometimes for the case where the user can't guess the number in 7 tries you can test it and see that even though the else is not after the if, when the if guess==secretNumber is true and you break the loop the else statement is not doing anything. P.S still new to answering here so please give feedback about my answer
import random
print ('Hello, What is your name?')
name = input()
print('Well, ' + name + '! I am thinking of a number between 1 to 20.')
secretNumber = random.randint(1,20)
print('Debug:' + str(secretNumber))
for guessTaken in range (1,7):
print('Take a guess. '+ name +'!, You Have 7 tries' )
try:
guess = int(input())
if guess<secretNumber :
print('Your guess is too low')
elif guess>secretNumber :
print('Your guess is too high')
if guess == secretNumber:
print('Good Job, ' + name + '! You guess my number in ' + str(guessTaken)+ ' guesses!')
break # This is for correct guess is equal.
except:
print('Please Enter A number ' + name + 'and not anything else' )
else:
print('Nope, The number I was thinking of was ' +str(secretNumber))
Upvotes: 0
Reputation: 7214
How about this, it re asks if the user inputs a string:
# 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 1 to 20.')
secretNumber = random.randint(1,20)
print('Debug:' + str(secretNumber))
for guessTaken in range (1,7):
print('Take a guess. '+ name +'!' )
try:
guess =int(input())
except:
print("Must be a number, try again: ")
continue
if guess<secretNumber:
print('Your guess is too low')
elif guess>secretNumber:
print('Your guess is too high')
else:
break # This is for correct guess is equal.
if guess ==secretNumber:
print('Good Job, ' +name+ '! You guess my number in ' + str(guessTaken)+ ' guesses!')
else:
print('Nope, The number I was thinking of was ' +str(secretNumber))
Upvotes: 1