Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's some code from a book that I have been working through since I am new to Python....this part works as it should

totalCost = 0
print 'Welcome to the receipt program!'
while True:
    cost = raw_input("Enter the value for the seat ['q' to quit]: ")
    if cost == 'q':
        break
    else:
        totalCost += int(cost)

print '*****'
print 'Total: $', totalCost
share|edit
answered 8 hours ago

My dilemma is....I need to validate what the input is...so if a user enters a string (like the word 'five' instead of the number) other than q or a number it tells them "I'm sorry, but 'five' isn't valid. please try again.....and then it prompts the user for the input again. I am new to Python and have been wracking my brains on this simple problem

*UPDATE** Since I don't have enough credits to add an answer to my own question for so log I am posting this here....

Thank you everyone for your help.  I got it to work!!!  I guess it's going to take me a little time to get use to loops using If/elif/else and while loops.

This is what I finally did

total = 0.0
print 'Welcome to the receipt program!'
while True:
    cost = raw_input("Enter the value for the seat ['q' to quit]: ")
    if cost == 'q':
        break
    elif not cost.isdigit():
        print "I'm sorry, but {} isn't valid.".format(cost)
    else:
        total += float(cost)
print '*****'
print 'Total: $', total
share|improve this question

6 Answers 6

if cost == 'q':
    break
else:
    try:
        totalCost += int(cost)
    except ValueError:
        print "Invalid Input"

This will see if the input is a integer and if it is, it will break. If it is not, it will print "Invalid Input" and go back to the raw_input. Hope this helps :)

share|improve this answer

You probably need to check if it isdigit:

>>> '123'.isdigit()
True

So that would be something like:

totalCost = 0
print 'Welcome to the receipt program!'
while True:
    cost = raw_input("Enter the value for the seat ['q' to quit]: ")
    if cost == 'q':
        break
    elif cost.isdigit():
        totalCost += int(cost)
    else:
        print('please enter integers.')

print '*****'

print 'Total: $', totalCost
share|improve this answer

You're already most of the way there. You have:

if cost == 'q':
    break
else:
    totalCost += int(cost)

So maybe add another clause to the if statement:

if cost == 'q':
    break
elif not cost.isdigit():
    print 'That is not a number; try again.'
else:
    totalCost += int(cost)

How can i check if a String has a numeric value in it in Python? cover this specific case (checking for a numeric value in a string) and has links to other, similar questions.

share|improve this answer
    
Ha, beat you to it! –  Aaron Hall Mar 1 '14 at 1:32

You can cast your input using :

try:    
    float(q)
except:
    # Not a Number
    print "Error"

If the input of user can only be integer with dot you can use q.isdigit()

share|improve this answer
1  
No... not Pokemon exceptions! cdn1.importnew.com/2013/11/Exception.jpg –  Aaron Hall Mar 1 '14 at 1:34
    
isdigit is great but you can't test if the input is a float. Furthermore you do something is except you alert user of wrong input ! –  lpoignant Mar 1 '14 at 1:37
if cost == 'q':
    break
else:
    try:
        totalCost += int(cost)
    except ValueError:
        print "Only Integers accepted"
share|improve this answer
    
Using exceptions for control flow is much more expensive than using ordinary if else control flow. –  Aaron Hall Mar 1 '14 at 1:35
    
@AaronHall: All the ordinary control flow in the world won’t help you if you’re using isdigit to check for floats, though. (Yes, the question changed.) –  minitech Mar 1 '14 at 2:11
    
If depend on what you expect to receive, if the case where cost isn't a digit is an exception (I mean, most of the time it will be a number) using a exception is less expensive. As I wasn't really sure about it, I made a test with this pastebin.com/nV4MLsHZ and got always better results with exceptions –  Ruben Bermudez Mar 1 '14 at 2:18
try:
    int(cost)
except ValueError:
    print "Not an integer"

I also recommend using:

if cost.lower() == 'q':

to still quit if it's a capital "Q".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.