Question

beginner python user! I'm writing a tip calculator that will add a tip to a given meal amount but I've ran into a problem. Here is the code I need help with.

bill_amt = True
while bill_amt:
    bill_amt = float(input('First, what was the price of your meal?(Please do not use signs such as "$"):'))
    if bill_amt <= 0: #or if bill_amt has letters or symbols
        print('Your meal wasn\'t $',bill_amt, '! Please try again.')
        bill_amt = True
    else:
        x = float(bill_amt)
        bill_amt = False

I would like to be able to add to if bill_amt <= 0: with a command that will also catch symbols(excluding periods .) and letters so that if you input a price of $54.65 or 56.ad then you will have to enter a proper input. Thanks, sorry if this is a duplicate! -Pottsy

Était-ce utile?

La solution

Something like this will work.

bill_amt = True
while bill_amt:
    try:
        bill_amt = float(input('First, what was the price of your meal?(Please do not use signs such as "$"):'))
    except ValueError:
        print('Please enter just a number')
        continue
    if bill_amt <= 0: #or if bill_amt has letters or symbols
        print('Your meal wasn\'t $',bill_amt, '! Please try again.')
        bill_amt = True
    else:
        x = float(bill_amt)
        bill_amt = False
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top