1

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
0

6 Answers 6

4
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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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()

2 Comments

No... not Pokemon exceptions! You don't want to catch 'em all.
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 !
0
if cost == 'q':
    break
else:
    try:
        totalCost += int(cost)
    except ValueError:
        print "Only Integers accepted"

1 Comment

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
0
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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.