如何做出无效选择。如果用户未输入选项之一,请重试打印。

时间:2018-10-30 17:12:32

标签: python

我正在编写仅模拟一个客户的银行应用程序的代码。我不知道该怎么做,但是问题是,如果用户没有输入D W BE,那么应该说Invalid Choice. Try again.

但是即使我输入了存款金额,它也会打印出来,一切都很好。因此,我的问题是,仅当用户输入Invalid Choice. Try again D WB而不是输入数字后,才使E打印吗?

这是我的代码:

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("*****")

2 个答案:

答案 0 :(得分:0)

在您的代码中,response仅分配了一次。永远是“ w”。因此,您的while循环可以只是while True:

在python中,您描述的条件可以表示为:

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

答案 1 :(得分:0)

您可以使用elif检查您的输入

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("*****")

或在输入验证中包含所有支票

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("*****")