0

Novice Pythoner here. I am trying to finish my first program (a tip calculator) and I have one last piece of code to write. Here is the part of code I need to add on to:

bill_amt = True
while bill_amt:
    try:
        bill_amt = float(input('First, what was the price of your meal?:'))
    except:
        print('Please enter a number only.')
        continue
    if bill_amt <= 0:
        print('Your meal wasn\'t $',bill_amt,'! Please try again.')
        bill_amt = True
    else:
        x = float(bill_amt)
        bill_amt = False

What I want to do is add a command that will limit the amount of numbers you can input when the code asks how much your meal was so user can't type in 4511511513545513513518451.32. I've tried using len(bill_amt) > 8, but I get an error that floats don't have strings. How do I get around this? Thanks, sorry if it's a duplicate! -Pottsy

3
  • 2
    should except ValueError: not just except Commented May 7, 2014 at 22:40
  • if bill_amt > 99999.99:? Commented May 7, 2014 at 22:44
  • @jonrsharpe 0.00000000000000000000000001 > 99999999 evaluates to False Commented May 7, 2014 at 22:45

3 Answers 3

1

use regex matching, this will also prevent the user from typing in something like "12.123456"

import re
# ...

while True:
    inp = input('First, what was the price of your meal?:'))
    if bool(re.match(r"^\d{1,8}\.\d\d$", inp)):
        return float(inp)
    else:
        print('invalid entry')

\d means digit, {1,8} means allow anywhere from 1 to 8 digits. \d\d looks for two digits after the ., so this regex will match 1-8 digits, followed by a dot, followed by two more digits.

Note that if you are dealing with money, you don't generally want to use floats, but rather decimal.Decimals. Try doing

decimal.Decimal(inp)

at the end instead.

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

Comments

0

In order to get the length use str(bill_amount)

len(str(123.53)) is 6

Note that even though Python will allow you to use a float value and then change it to the boolean, it would be better to make it a different name rather than have both the float and the boolean as bill_amt .

Comments

0

Float doesn't have length but str does:

bill_amt_str = input('First, what was the price of your meal?:')
if len(bill_amt_str.replace(".", "")) > 8:
    print("No more than 8 digits please.")
    continue
bill_amt = float(bill_amt_str)

7 Comments

Or, use in-place conversion without adding extra variable; having bill_amt as it was, just check if len(str(bill_amt))>8:
@GMGray garbage collector will wipe out this tiny little string anyway.
@GMGray there's an extra name but no more space allocated for the variable
@Ryan Haining - of course, it's just a matter of personal coding preferences.
@ElmoVanKielmo - could you explain where are potential problems with this shortcut? I'd have to get rid of bad habits then..
|

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.