The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

This topic is READ ONLY
kamaboko (5) [Avatar] Offline
#1
Hello,

I've included a very simple program to help with my question. I want to know how I account for incorrect input. For instance, in question one I'm asking for a string input. I'd like it to check to make sure a string is input, and if not, to tell the user to input a string. The same goes for questions two and three, but for integers.

Thanks,
K


# This will ask three questions. The user will input an answer. At the end
# it will tell the user how many questions they got right and the percentage


ans1 = "oregon"
ans2 = 5
ans3 = 2

count = 0

q1 = str(raw_input("What state do I live in? ")) # question 1
if q1.lower() == ans1:
print "Great Job"
print
count = count + 1
else:
print "Wrong"
print


q2 = int(raw_input("How many fingers on your right hand? ")) # question 2
if q2 == ans2:
print "Correct"
print
count = count + 1
else:
print "Nope"
print


print "Who sings 'Living After Midnight'? " # question 3
print "1. Angle City"
print "2. Judas Priest"
print "3. Cream"
print "4. Jewel"
print "5. Y&T"
q3 = int(raw_input("Enter number:"))
if q3 == ans3:
print "Very good!"
print
count = count + 1
else:
print "Maybe you don't listen to many bands"
print

percentage = ((count / 3.0) * 100)

print "You answered", count, "correctly. That's", round(percentage, 2), "percent right"
carter.sande (32) [Avatar] Offline
#2
Re: error handling question
You can put your code in a try-except block, like this:

try:
answer = int(raw_input("Type an integer: "))
except:
print "You didn't type an integer!!!"
else:
print "You typed in an integer."

This will check if Python can convert the input to an integer. If it can't, it will tell the user.

Carter
kamaboko (5) [Avatar] Offline
#3
Re: error handling question
Thanks for the reply. I tried your suggestion, and it works, but I want to know how to tweak it.

For example, let's say a user enters 't' for what should be an integer. I'd like a message to appear telling them that they didn't enter an integer, and to make another entry. In the solution that you suggested, it doesn't do that. Is there a way to do so?

Thanks,
K
carter.sande (32) [Avatar] Offline
#4
Re: error handling question
K,

You'd probably want to put the type-checking in a while loop. Then, you could ask the user over and over and over again until they finally decide to give your program a valid integer.
Here's some code that would do that.

input_valid = False
while not input_valid:
....q2 = (raw_input("What's your age?"))
....try:
........q2int = int(q2)
....except:
........print "You didn't enter an integer. Please try again."
....else:
........input_valid = True

Replace the dots with spaces, because this forum eats whitespace.

From,
Carter
campbell_b (3) [Avatar] Offline
#5
Re: error handling question
Carter,

While reading your replies for this error handling question you used the expression "this forum eats whitespace." I am not famailar with that expression. Briefly, what does it mean to "eat whitespace"? Thanks.

Bryan
carter.sande (32) [Avatar] Offline
#6
Re: error handling question
Bryan,

When I said it "eats whitespace", I meant it got rid of the spaces in the code.

Sorry about the confusion!

Carter
warren.sande (211) [Avatar] Offline
#7
Re: error handling question
Specifically, tabs and spaces at the start of each line.


- Warren Sande
campbell_b (3) [Avatar] Offline
#8
Re: error handling question
Hence the need for the dots to indicate spaces and indentation. I get it now. Thanks for replying. Thanks also to Warren for following up.

Bryan