1

How can i get only a money value input with out splitting dollars and cents?

I tried .isdigit() but then i can only use int not floats.

def deposit():
    deposit_amount = 0
    print('How much would you like to deposit?')
    print('No decimal')
    deposit_dollars = input('Dollar amount: ')   
    if deposit_dollars.isdigit():
        print('Double digit ex: 02 = 2 cents')
        deposit_cents =input('Cents: ') 
        if deposit_cents.isdigit():
            if int(deposit_cents) <= int(99):
                f = open(login+'.txt','a')
                i = datetime.now() 
                f.write(deposit_dollars+'.'+deposit_cents+','+i.strftime('%Y/%m/%d %H:%M:%S')+','+'Deposit'+'\n')
                f.close() 
                display_menu()

this code works but I'd like to have the input be in one entry. ex: 23.45. while not allowing a random letter or symbol other that decimal point.

Jeremy
  • 11
  • 1
  • 1
    Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – wjandrea Apr 27 '19 at 23:00
  • TL;DR of duplicate: `amount = float(input('How much would you like to deposit? '))` – wjandrea Apr 27 '19 at 23:01
  • right, but how do i address the error when a string is entered? – Jeremy Apr 27 '19 at 23:11
  • Well, your current code fails silently, which is worse than an error. But for handling exceptions, use a [`try`-`except` block](https://docs.python.org/3.7/tutorial/errors.html#handling-exceptions). – wjandrea Apr 27 '19 at 23:21
  • 2
    FYI, it usually not a good idea to use `float` to store money. You may experience rounding errors. It's best to store the money in the lowest denomination as an integer. In the US that would be cents. – 001 Apr 27 '19 at 23:23
  • @JohnnyMopp thanks for the advice ill change that – Jeremy Apr 28 '19 at 04:15
  • @wjandrea Your last comment with the try: except: worked. I'm still very new and learning. – Jeremy Apr 28 '19 at 05:09

0 Answers0