1
votes

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
6

6 Answers

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

0
votes

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
0
votes

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.

0
votes

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

0
votes
if cost == 'q':
    break
else:
    try:
        totalCost += int(cost)
    except ValueError:
        print "Only Integers accepted"
0
votes
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".