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天),写入文件。只能进行本次账单的打印,通过列表进行输出,没有将账单写入文件。需要进一步优化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值