0

Im currently working on a project for my coding class. The prompt is to make an atm interface.

All of my code is currently working but when it comes to the function of deposit() it asks for a whole number to be entered where I use int(input) say someone inputs a float like 45346.4 it comes up with an error. Is there a fix to this?

here is my code currently for the deposit function. The balance is already given outside of this function.

def deposit():
    balanced = balance
    print(f'Your current balance is ${balanced}\n------------------------------')
    print('How much money would you like to deposit?\n---------------------------\n You can only deposit in whole numbers')
    deposit_amount = int(input('Enter here:'))   
    
    if deposit_amount.is_integer():                      
        balance_a_d = balanced + deposit_amount
        print(f'You Current Balance is ${balance_a_d}\n-------------------------------\nHave a great day!')
        quit()
    else:
        print('----------------------------\nThat is not a whole number please try again\n----------------------------')
        deposit()
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Why is that a problem? They're supposed to enter a whole number, and a float with a fraction isn't a whole number. – Barmar Jun 16 '22 at 19:17
  • 1
    If you want to ignore the fraction you can use `int(float(input(...)))` – Barmar Jun 16 '22 at 19:17
  • Why are yuo only allowing whole numbers? Let them enter decimals with `float(input(...))` – Barmar Jun 16 '22 at 19:18
  • You should also allow for any other unexpected input such as 'foo' – DarkKnight Jun 16 '22 at 19:18
  • Hi, @Barmar Thank you for your response!. My brain was thinking it was a problem because say someone was like okay what if I put in a float and didn't care about the guidelines it would error. My worry would be for a case like that. I am also going off that you can only insert bills and not coins. – TylerDr16 Jun 16 '22 at 19:19
  • Side-note: For monetary calculations, you don't even want `float` (which is base 2, and can't represent base 10 floating point math precisely). The `decimal.Decimal` class is what you'd want in any real code. – ShadowRanger Jun 16 '22 at 19:23
  • @ShadowRanger How do you use decimal.decimal Im new to python so I do not know all the capabilities of it yet – TylerDr16 Jun 16 '22 at 19:25
  • @AlbertWinestein How would you check for that as well? – TylerDr16 Jun 16 '22 at 19:25
  • There should be examples in the documentation. – Barmar Jun 16 '22 at 19:25
  • @TylerDr16: [See the `decimal` docs](https://docs.python.org/3/library/decimal.html). For basic usage, it's just `from decimal import Decimal`, then using `Decimal` in place of `float`, and replacing any relevant `float` literals like `3.14159` with `Decimal("3.14159")` (note the argument to `Decimal` is a `str`, not a `float`; if you pass a `float`, `Decimal` faithfully reproduces the imprecision of the `float` and you gain little or nothing). – ShadowRanger Jun 16 '22 at 19:40
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – ti7 Jun 19 '22 at 22:14

2 Answers2

0

You could use a while loop and a try block to keep asking for input until ValueError is no longer raised.

Something like this:

number = None
while number == None:
    try:
        number = int(input('Enter here:'))
    except ValueError as e:
        print('Please enter a whole number.')
  • Hi! That worked perfectly, I have a questions. What does the e represent in the line except ValueError as e: – TylerDr16 Jun 16 '22 at 19:29
  • e is just the variable that holds that error so if you were to `print(type(e), e)` it would say something like "ValueError invalid literal for int() with base 10". – Connor Dudley Jun 16 '22 at 19:34
  • Hi Thank you for your Response! I shall try that. Im thinking of rebuilding the code so I will take that into accountance – TylerDr16 May 04 '23 at 18:56
0

I have a general purpose input function which is simple to use and saves a lot of repeated code.

def getInput(prompt, t=str):
    while True:
        v = input(f'{prompt}: ')
        try:
            return t(v)
        except ValueError:
            print('Invalid input')

If I just want text input then:

getInput('Enter some text')

Of course, for plain text, this isn't really necessary.

If I want a float then:

getInput('Enter a float', float)

Or int:

getInput('Enter an integer', int)

This saves you from having to "wrap" all your inputs in try/except as it's all handled for you

DarkKnight
  • 19,739
  • 3
  • 6
  • 22