如何用while循环完成这个Python银行程序?

时间:2018-01-29 06:40:24

标签: python python-3.x

这是指令。

  

编写程序以模拟银行交易。有两个银行账户:支票和储蓄。

     

从checking_balance = 500和savings_balance = 2000开始。包含四个选项供用户执行以下事务。打印当前余额(包括支票和储蓄)。存款(仅限支票)。撤回(仅限检查)。转移(从保存到仅检查)。退出程序

这是我到目前为止所做的。我对如何完成这个程序感到茫然。

#author 
#date January 2, 2018
#title Bank Transaction 

def bank():
while True:
    checking_balance = 500
    savings_balance = 2000
    newCheck = 500 + 0

    choice = input("""Choose one of the choices below.
    d(deposit) p(print current balance) w(withdraw)
    t(transfer) q(quit): """)
    if (choice == 'q' or choice == 'Q'):
        break #exit loop
        end

    if (choice == 'd' or choice == 'D'):
        deposit = float(input("Put amount: "))
        newCheck = checking_balance + deposit
        checking_balance = newCheck
       # print("New checking balance is $%.2f"%newCheck)

    if (choice == 'p' or choice == 'P'):
        print ("New checking balance is $%.2f"%newCheck)

    if (choice == 'w' or choice == 'W'):
        withdraw = float(input("Put amount: "))
        newCheck = checking_balance - withdraw
        checking_balance = newCheck




    #wait = input("press any key to exit.")

1 个答案:

答案 0 :(得分:0)

接受你的任务老兄,但请不要问解释一下你是否不知道python的基础......这只是一个帮助

import sys

checking_bal = 2000
savings_bal = 500

while(1):

    print "________________________________\nPress 1---> For_withdraw_from_checking\nPress 2---> For_deposit_in_checking\nPress 3---> transfer_frm_checking_to_saving\nPress 4---> print_all_balance\npress 5--> For_Exit\n________________________________"
    input = raw_input("Please Enter your choice")

    if input == '1':
        amt = int (raw_input("Enter amount to be withdraw_from_checking account"))
        checking_bal -= amt

    elif input == '2':
        amt = int (raw_input("Enter amount to be deposit_in_checking account"))
        checking_bal += amt

    elif input == '3':
        amt = int (raw_input("Enter amount to be transfer_frm_checking_to_saving account"))
        savings_bal += amt
        checking_bal -= amt
    elif input == '4':
        print "Checking Balance--> ", checking_bal
        print "Saving Balance--> ", savings_bal
    elif input == '5':
        print "Exiting..."
        sys.exit(0)
    else:
        print "Please Enter valid choice    Try again..."

请在您的电脑上运行此代码以便更好地理解。这很容易。