Python運維基礎(3)程序大練習(ATM)

較大練習程序

ATM

功能要求:
1. 用戶登錄
2. 額度15000
3. 可以提現,提現手續費5%
4. 每月最後一天出賬單(每月30天),寫入文件
5. 記錄日常消費流水
6. 還款
7. 密碼鎖定

f = open("accountinfo.txt")
count = 3
account_dic = {}
limit_dic = {}
balance_dic = {}
list_account = []

"""
額度查詢
"""


def fun_1(account):
    print("\033[34;1mThe limit of this account is %s .\033[0m" % limit_dic[account])


"""
用戶提現(提現手續費5%)
"""


def fun_2(account):
    while True:
        cash = int(input("Please input how much money you want to withdraw:"))
        if cash % 100 == 0 & cash <= 2000:
            fee = 0.05 * cash  # 手續費
            list_account.append('cash', )  # 賬單(項目)
            list_account.append(cash)  # 賬單(花費)
            list_account.append('fee', )  # 賬單(項目)
            list_account.append(fee)  # 賬單(花費)
            balance_dic[account] = str(int(balance_dic[account]) - int(1.05 * cash))
            print("\033[34;1mThe balance of this account is %s .\033[0m" % balance_dic[account])
            break
        else:
            print("Sorry, please change the number of money:")


"""
商城服務
"""


def fun_3(account):
    products = []
    price = []
    shop_list = []
    m = open('list.txt')
    for e_line in m.readlines():
        new_line = e_line.split()
        products.append(new_line[0])
        price.append(new_line[1])
    print("Welcome to python shop~\tThe list of products is here : ")  # 打印商品信息
    for p in products:
        p_index = products.index(p)
        p_price = price[p_index]
        print(p, p_price)
    while True:
        money = int(balance_dic[account])
        e_choice = input("Please choose the product that you want to buy:")
        if e_choice in products:
            index = products.index(e_choice)
            e_price = int(price[index])
            if money >= e_price:
                shop_list.append(e_choice)
                list_account.append(e_choice)
                list_account.append(e_price)
                money = money - e_price
                balance_dic[account] = str(money)
                print("The product you choose has been added in the shop_list ~ ")
                print("You will take %d to buy %s, and your balance is %s" % (e_price, e_choice, balance_dic[account]))
                question = input("\033[34;1mDo you want to keep buying ? yes/no\033[0m")
                if question == 'yes':
                    continue
                else:
                    break
            else:
                if money >= int(min(price)):
                    print("\033[36;1mYou can't afford it, please choose another one\033[0m")
                else:
                    print("\033[36;1mSorry, you don't have enough money, bye ~\033[0m")
                    break


"""
查詢賬單
"""


def fun_4(account):
    print("##########THIS IS YOUR SHOP LIST##########")
    print(list_account)


"""
用戶還款
"""


def fun_5(account):
    tip = int(input("Please input the number of money that you want to back : "))
    balance_dic[account] = str(int(balance_dic[account]) + tip)
    print("\033[34;1mDear client, you have back %d yuan successfully ! \033[0m" % tip)
    print("\033[36;1mYour balance is %s yuan~\033[0m" % balance_dic[account])


for line in f.readlines():
    accountinfo = line.split()[0]  # 賬戶
    accountcode = line.split()[1]  # 密碼
    limit = line.split()[2]  # 額度
    balance = line.split()[3]  # 餘額
    account_dic[accountinfo] = accountcode
    limit_dic[accountinfo] = limit
    balance_dic[accountinfo] = balance
#  ERROR!! 這裏注意:打開了整個文件,一行一行進行讀取,每一次都對limit的值進行改變,如果fun_1()的print裏輸出limit的值,結果無論如何都只能是文件最後一行的值!!!
Accountinput = input("Please input your account:").strip()
# 支持多用戶登錄
while True:
    if account_dic.__contains__(Accountinput):
        code = input("Please input your code : ").strip()
        if account_dic[Accountinput] == code:
            print("########## WELCOME TO PYTHON BANK ! ##########")
            print("Here we have such six functions :")
            print("\033[32;1m1.額度查詢\t2.用戶提現\t3.商城服務\t4.查詢賬單\t5.用戶還款\t6.退出系統\033[0m")
            while True:
                choice = int(input("Please input your choice : ").strip())
                if choice == 1:
                    print("額度查詢:")
                    fun_1(Accountinput)
                elif choice == 2:
                    print("用戶提現:")
                    fun_2(Accountinput)
                    while True:
                        answer = input("Do you want to keep withdrawing the money ? yes/no")
                        if answer == 'yes':
                            fun_2(Accountinput)
                        else:
                            break
                elif choice == 3:
                    print("商城服務:")
                    fun_3(Accountinput)
                elif choice == 4:
                    print("查詢賬單:")
                    fun_4(Accountinput)
                elif choice == 5:
                    print("用戶還款:")
                    fun_5(Accountinput)
                else:
                    print("\033[34;1m##########退出系統,bye~##########\033[0m")
                    exit()

        else:
            count = count - 1
            print("You only have %s times to input it" % count)
            if count == 0:
                print("\033[36;1mThis account has been locked !\033[0m")
                break
    else:
        print("The account that you input is not here, please check it and try again ! ")
        while True:
            Accountinput = input("Please input your account:")
            break

上述程序中某些功能還不是很完善,比如:每月最後一天出賬單(每月30天),寫入文件。只能進行本次賬單的打印,通過列表進行輸出,沒有將賬單寫入文件。需要進一步優化。

發佈了31 篇原創文章 · 獲贊 222 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章