python使用文件处理加函数的方式写ATM简易操作

  1 import os
  2 
  3 
  4 # 充值功能
  5 def pay_money():
  6     name_inp = input("请输入要充值的账号:").strip()
  7     # 调用查询功能查询输入的用户是否存在,若存在将返回账户的余额
  8     res = pay_check(name_inp)
  9     res1 = int(res)
 10     # print(res)
 11     while 1:
 12         money_inp = input("请输入充值金额:").strip()
 13         # 判断输入的金额是否是数字,不是则重新输入
 14         if not money_inp.isdigit():
 15             print("请输入的必须是数字".center(50,"*"))
 16             continue
 17         money_inp = int(money_inp)
 18 
 19         res1 += money_inp
 20         # dic[name_inp] += money_inp
 21         print("充值成功".center(50,"*"))
 22         # 打开文件,将文件内容读取到内存中。若数据大,则推荐使用os模块
 23         # 将新的数据写入copy文件然后重命名
 24         with open("db.txt", "r", encoding="utf8") as f:
 25             dic = {}
 26             for line in f:
 27                 user, money = line.strip().split(":")
 28                 dic[user] = int(money)
 29             if name_inp in dic:
 30                 dic[name_inp] = res1
 31             else:
 32                 return
 33         with open("db.txt", "w", encoding="utf8") as w:
 34             for user, money in dic.items():
 35                 w.write("%s:%s\n" % (user, money))
 36         break
 37 
 38 # 转账功能
 39 def transfer():
 40     A_name = input("请输入要转账的账户:").strip()
 41     res_A = pay_check(A_name)
 42     if res_A < 0:
 43         return
 44     B_name = input("请输入要接收的账户:").strip()
 45     res_B = pay_check(B_name)
 46     if res_B < 0:
 47         return
 48 
 49     money = int(input("请输入要转账的金额:").strip())
 50     res_A1 = int(res_A)
 51     res_B1 = int(res_B)
 52 
 53     if res_A1 > money:
 54         res_B1 +=money
 55         res_A1 -=money
 56         print(res_A1,res_B1)
 57         print("转账成功".center(50,"*"))
 58 
 59     with open("db.txt", "r", encoding="utf8") as f:
 60         dic = {}
 61         for line in f:
 62             user, money = line.strip().split(":")
 63             dic[user] = int(money)
 64         dic[A_name] = res_A1
 65         dic[B_name] = res_B1
 66     with open("db.txt", "w", encoding="utf8") as w:
 67         for user, money in dic.items():
 68             w.write("%s:%s\n" % (user, money))
 69 
 70 # 取现功能
 71 def pay_cash():
 72     A_name = input("请输入要提现的账户:").strip()
 73     res_A = pay_check(A_name)
 74     if res_A < 0:
 75         return
 76 
 77     while 1:
 78         money = input("请输入要提现的金额:").strip()
 79         if not money.isdigit():
 80             print("输入的金额必须是数字".center(50,"*"))
 81         money = int(money)
 82 
 83         if money > res_A:
 84             print("提现金额不足...".center(50,"*"))
 85         else:
 86             res_A -=money
 87             print("取现成功".center(50,"*"))
 88 
 89         with open("db.txt", "r", encoding="utf8") as f:
 90             dic = {}
 91             for line in f:
 92                 user, money = line.strip().split(":")
 93                 dic[user] = int(money)
 94             # if A_name in dic:
 95             dic[A_name] = res_A
 96         with open("db.txt", "w", encoding="utf8") as w:
 97             for user, money in dic.items():
 98                 w.write("%s:%s\n" % (user, money))
 99         break
100 
101 # 查询功能
102 def pay_check(name):
103     dic = {}
104     with open("db.txt", "r", encoding="utf8") as f:
105         for line in f:
106             user, money = line.strip().split(":")
107             dic[user] = int(money)
108 
109     if name not in dic:
110         print("用户不存在")
111         choice = input("是否需要注册账户(Y/N):").strip()
112         if choice.upper() == "Y":
113             pay_register()
114         else:
115             print("结束本次操作".center(50,"*"))
116             return -1
117     else:
118         print("账户【%s】的余额【%s】" % (name, dic[name]))
119         return dic[name]
120 
121 
122 # 注册功能
123 def pay_register():
124     username = input("请输入你要注册的账户:").strip()
125     while 1:
126         money_inp = input("请输入你要充值的金额:").strip()
127         if not money_inp.isdigit():
128             print("请输入数字!".center(50,"*"))
129             continue
130         else:
131             print("充值成功!".center(50,"*"))
132             money_inp = int(money_inp)
133 
134 
135         with open("db.txt","a",encoding="utf8") as f:
136             f.write("%s:%s\n"%(username,money_inp))
137         return
138 
139 
140 if __name__ == "__main__":
141     # transfer()
142     # pay_money()
143     # pay_check("alex2")
144     # pay_cash()

 

上一篇:strip()的正则表达式版本


下一篇:直播新架构升级:全量支撑淘宝双11直播