python ATM循环麻烦

时间:2014-12-12 22:43:20

标签: python

我已经编写了这个程序,似乎无法弄清楚如何让程序循环回到开头并询问“选择”#39;再次选择。

程序运行正常,所有内容都打印到屏幕上,甚至是询问您是否需要其他事务的部分,如何让它循环回来?

  

写ATM程序。输入帐户余额,打印期初余额。   根据选择,呼叫功能要求存款或取款   执行他们想要的操作,然后打印新的余额。 (使用迭代)

def withdraw():
    amt = int(input("What amount to withdraw - multiples of $20: "))
    print('New balance: $', balance - amt)


def deposit():
    amt = int(input("How much are you depositing? "))
    print('New balance: $',balance + amt)

def bal():
    return(balance)


print ("Hello, Welcome to Python ATM.")

balance = float(65.01)

pin = int(input("please enter your account number (Any number:) "))

print('''Current balance: $''',balance)

choice = int(input('''
Please choose an option from the following:
1 - Withdraw
2 - Deposit
3 - Check Balance
4 - Exit:  '''))


if choice == 1:
    print(withdraw());
elif choice == 2:
    print(deposit());
elif choice == 3:
    print(bal());

more = input("Would you like another transaction? (y/n)")

1 个答案:

答案 0 :(得分:1)

也许你需要一个循环来重复选择:

while True:
    print('''Current balance: $''',balance)

    choice = int(input('''
Please choose an option from the following:
1 - Withdraw
2 - Deposit
3 - Check Balance
4 - Exit:  '''))


    if choice == 1:
        print(withdraw());
    elif choice == 2:
        print(deposit());
    elif choice == 3:
        print(bal());


    more = input("Would you like another transaction? (y/n)")
    if more.lower() != 'y':
        print("Goodbay")
        break