1

I am trying to build an ATM code , but I have another piece of code that is again proving troublesome. Here is the error:

How Much Funds Do You Wish To Input Into Your Account? : £1565
Not A Vaild Amount

I am trying to make any integer be an acceptable answer, here is the code I created to check the answer:

def inputFunds(self):
    funds_input =(input("How Much Funds Do You Wish To Input Into Your Account? : £"))
    num_check = isinstance(funds_input, float)
    if num_check == "True" :
                         self.balance = self.balance + funds_input
                         print("Input Complete. Your New Balance is £" + self.balance)
    else:
        print("Not A Vaild Amount")
        tryagain =(input("Do You Wish To Try Again?"))
        if tryagain in ("Y", "y", "Ye", "ye", "Yes", "yes"):
            print(atm.inputFunds())
        else:
            back_menu =(input("Do You Wish To Go Back To The Menu? "))
            if back_menu in ("Y", "y", "Ye", "ye", "Yes", "yes"):
                print(atm.Menu())
            else:
                print(atm.End())

This is the last thing I need to make my code run smoothly. Many Thanks.

EDIT

Having tried both the other answer that has been linked , and also the advice given below. My code still gives an syntax error to the 'except' statement. Here is my code now:

    def inputFunds(self):
    try:
        funds_input = int(input("How Much Funds Do You Wish To Input Into Your Account? : £"))
        self.balance = self.balance + funds_input
        print("Your New Balance Is £" + str(self.balance)
    except ValueError:
        print("Not A Valid Amount")
        tryagain = input("Do You Wish To Try Again? ")
        if tryagain in ("Y", "y", "Ye", "ye", "Yes", "yes"):
            print(atm.inputFunds())
        else:
            back_menu = input("Do You Wish To Go Back To The Menu? ")
            if back_menu in ("Y", "y", "Ye", "ye", "Yes", "yes"):
                print(atm.Menu())
            else:
                print(atm.End()

1 Answers1

0

You cannot do:

num_check = isinstance(funds_input, float)

because input always returns a string object. isinstance is used only to test the type of an object, not to see if a string is a representation of another type.

Instead, you need to explicitly convert the input into a float:

funds_input = float(input("How Much Funds Do You Wish To Input Into Your Account? : £"))

Of course, you may also want to wrap this in a try/except to handle non-numerical inputs:

try:
    funds_input = float(input("How Much Funds Do You Wish To Input Into Your Account? : £"))
except ValueError:
    # Handle error

For more information, see: Asking the user for input until they give a valid response

Community
  • 1
  • 1
  • Hi @iCodez , thanks for your help. I just edited my question, if you can please look at it I would really appreciate it. –  Nov 19 '14 at 19:01
  • 1
    You are missing a closing parenthesis on the `print("Your New Balance Is £" + str(self.balance)` line. –  Nov 19 '14 at 19:11