0

I have a code to make a banking app. I need to make sure that the input can take in any number including decimals but not take in letters or other symbols.

Ive tried using a float instead of int

if selection == 1:
    if initial_balance > 1:
        print("\nAn account has already been opened. Please select another 
        option.")
        print(menu)
    else:
        name = input("\nEnter the account owner's name: ")

        # While loop to make sure user puts valid initial deposite
        while True:
            initial_balance = input("Enter your initial balanc: $")
            try:
               float(initial_balance)
            except ValueError:
                print("Sorry, Please deposit one or more dollars.")
                continue
            if initial_balance < 1:
                print("Please deposit one or more dollars.")
                continue
            else:
                balance += initial_balance
                print("\nAccount owner: " + name)
                account = BankAccount(initial_balance)
                print("Initial balance: $ " + str(initial_balance))
                print(menu)
                break
                break

expected: enter an initial balance : 20.75

account owner: jimmy initial balance: $20.75

actual:

enter an initial balance : $20.75

sorry please deposit one or more dollars

  • Is it possible that you can provide a more complete version of the code for testing? I don't think the current code can generate the "actual" results you provided. Based on your tag, it is in Python 3. Then `initial_balance < 1` is not working, as initial_balance would be a string. If you can change `print("Please deposit one or more dollars.")` to `print("You just input", repr(initial_balance))`, you might get some clues about what is going on. – nocte107 Jul 07 '19 at 01:50
  • 2
    Possible duplicate of [How do I check if a string is a number (float)?](https://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float) –  Jul 07 '19 at 03:03

1 Answers1

1
while True:
        initial_balance = input("Enter your initial balanc: $")
        try:
           float(initial_balance)

You are applying float to the initial balance, but you are not updating the variable, so it remains in string format. Below is a fixed version.

while True:
        initial_balance = input("Enter your initial balanc: $")
        try:
           initial_balance = float(initial_balance)
Zander0416
  • 21
  • 5