-1

I am writing code that simulates a bank application for just one customer. I got it all down, but the problem is that if the user doesn't input D W B or E then it is supposed to say Invalid Choice. Try again.

But it prints this even after I put in the deposit amount and everything is fine. So my question is on how to make Invalid Choice. Try again print only if the user inputs something other than D W B or E and not after inputting a number?

Here's my code:

response = "D"
balance = 0
deposit = 0
withdraw = 0
while (response=="D") or(response=="W") or (response=="B"):
    print("Type D to deposit money")
    print("Type W to withdraw money")
    print("Type B to display Balance")
    print("Type E to exit")
    ask = input("Enter your choice now: ")
    if(ask=="D"):
        deposit = int(input("Please enter your amount to deposit: "))
        balance = float(balance + deposit)
    if(ask=="W"):
        withdraw = int(input("Please enter the amount you want to withdraw: "))
        balance = float(balance - withdraw)
        if(balance<withdraw):
            print("Not enough balance")
    if(ask=="B"):
        print("Your balance is: " +str(balance))
    if(ask=="E"):
        break
    else:
        print("Invalid Choice. Try again")
    print("*****")
GhostCat
  • 137,827
  • 25
  • 176
  • 248
H. Khan
  • 51
  • 8
  • 1
    Possible duplicate of [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) – roganjosh Oct 30 '18 at 17:14
  • Maybe use `elif` instead of multiple `if` so the `else` statement wouldn't be reached each time `ask` is different from `E` – Tony Pellerin Oct 30 '18 at 17:23
  • Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. Feel free to drop me a comment in case you have further questions or feedback for me. – GhostCat Oct 30 '18 at 18:54

2 Answers2

0

In your code, response is only assigned once. It will always be "w". So your while loop can just be while True:

In python the condition you described could be expressed as something like:

list_of_valid_inputs = ["D", "W", "B", "E"] + [str(k) for k in range(10)]

if ask not in list_of_valid_inputs:
    foo() # Your code here
Charles Landau
  • 4,187
  • 1
  • 8
  • 24
0

You can use elif to check your input

ask = "D"
balance = 0
deposit = 0
withdraw = 0
while (ask =="D") or(ask =="W") or (ask =="B"):
    print("Type D to deposit money")
    print("Type W to withdraw money")
    print("Type B to display Balance")
    print("Type E to exit")
    ask = input("Enter your choice now: ")
    if(ask=="D"):
        deposit = int(input("Please enter your amount to deposit: "))
        balance = float(balance + deposit)
    elif(ask=="W"):
        withdraw = int(input("Please enter the amount you want to withdraw: "))
        balance = float(balance - withdraw)
        if(balance<withdraw):
            print("Not enough balance")
    elif(ask=="B"):
        print("Your balance is: " +str(balance))
    elif(ask=="E"):
        break
    else:
        print("Invalid Choice. Try again")
    print("*****")

Or surround all your checks with an input validation

ask = "D"
balance = 0
deposit = 0
withdraw = 0
while (ask =="D") or(ask =="W") or (ask =="B"):
    print("Type D to deposit money")
    print("Type W to withdraw money")
    print("Type B to display Balance")
    print("Type E to exit")
    ask = input("Enter your choice now: ")
    if ask in ('D','B','W','E'):
        if(ask=="D"):
            deposit = int(input("Please enter your amount to deposit: "))
            balance = float(balance + deposit)
        if(ask=="W"):
            withdraw = int(input("Please enter the amount you want to withdraw: "))
            balance = float(balance - withdraw)
            if(balance<withdraw):
                print("Not enough balance")
        if(ask=="B"):
            print("Your balance is: " +str(balance))
        if(ask=="E"):
            break
    else:
        print("Invalid Choice. Try again")
    print("*****")
Xnkr
  • 564
  • 5
  • 16