Python 小案例實戰 —— 簡易銀行存取款查詢系統


Python 小案例實戰 —— 簡易銀行存取款查詢系統

涉及知識點

  • 包的調用
  • 字典、列表的混合運用
  • 列表元素索引、追加
  • 基本的循環與分支結構

源碼

import sys
import time

bank = {
    'users':['Tom','Jack'],
    'pwd': ['1701', '1702'],
    'money':[1000,2000],
    'history':[[],[]]
}

while True:
    user_now_name = str(input("歡迎使用本系統!請輸入您的用戶名:\n"))
    if user_now_name in bank['users']:
        user_index = bank['users'].index(user_now_name)
        # print('尊敬的', user_now_name, '您好!')
        while True:
            user_now_pwd = str(input("請輸入您的密碼:\n"))
            if user_now_pwd == bank['pwd'][user_index]:
                print('登錄成功!')
                isLogin = True
                bank['history'][user_index].append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '         登陸系統')
                break
            else:
                print('密碼錯誤,請重試!\n')
        break
    else:
        print('抱歉,不存在該用戶!\n')


while isLogin:
     print('\n請選擇您要辦理的業務:1.取款,2.存款,3.查詢,4.退卡')
     service_num = int(input())
     if service_num == 1:
        money_out = int(input('請輸入取款金額:'))
        if money_out > 0 and money_out < int(bank['money'][user_index]):
            bank['money'][user_index]  = int(bank['money'][user_index]) - money_out
            print('當前剩余存款:', bank['money'][user_index])
            bank['history'][user_index].append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '         取款' + str(money_out))
        else:
            print('當前余額不足!!')
     elif service_num == 2:
        money_in = int(input('請輸入存款金額:'))
        if money_in < 0:
            print('存款金額必須大於0')
        else:
            bank['money'][user_index]  = int(bank['money'][user_index]) + money_in
            print('當前剩余存款:', bank['money'][user_index])
            bank['history'][user_index].append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '         存款' + str(money_in))
     elif service_num == 3:
         for record in bank['history'][user_index]:
            print(record)
     elif service_num == 4:
         bank['history'][user_index].append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '         退出系統')
         break

運行測試結果

歡迎使用本系統!請輸入您的用戶名:
GShang
抱歉,不存在該用戶!

歡迎使用本系統!請輸入您的用戶名:
Tom
請輸入您的密碼:
1702
密碼錯誤,請重試!

請輸入您的密碼:
1701
登錄成功!

請選擇您要辦理的業務:1.取款,2.存款,3.查詢,4.退卡
1
請輸入取款金額:1200
當前余額不足!!

請選擇您要辦理的業務:1.取款,2.存款,3.查詢,4.退卡
1
請輸入取款金額:200
當前剩余存款: 800

請選擇您要辦理的業務:1.取款,2.存款,3.查詢,4.退卡
2
請輸入存款金額:800
當前剩余存款: 1600

請選擇您要辦理的業務:1.取款,2.存款,3.查詢,4.退卡
1
請輸入取款金額:1700
當前余額不足!!

請選擇您要辦理的業務:1.取款,2.存款,3.查詢,4.退卡
1
請輸入取款金額:100
當前剩余存款: 1500

請選擇您要辦理的業務:1.取款,2.存款,3.查詢,4.退卡
3
2019-12-20 10:40:47         登陸系統
2019-12-20 10:40:57         取款200
2019-12-20 10:41:01         存款800
2019-12-20 10:41:14         取款100

請選擇您要辦理的業務:1.取款,2.存款,3.查詢,4.退卡
4

Process finished with exit code 0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM