zoukankan      html  css  js  c++  java
  • python使用文件处理加函数的方式写ATM简易操作

      1 # import os
      2 # 这里的os模块没有调用,如果是数据量大的话,则需要这个模块
      3 '''
      4 简易版的ATM功能,实现用户充值,转账等功能
      5 文件读取和存取方式采用的是读写同一个文件
      6 优点:占用磁盘空间少
      7 缺点:占用内存空间
      8 ps:也可以采用os模块对文件操作
      9 优点:占用内存空间少
     10 缺点:占用磁盘空间多
     11 两种方式根据自己需求选用!
     12 '''
     13 
     14 # 充值功能
     15 def pay_money():
     16     name_inp = input("请输入要充值的账号:").strip()
     17     # 调用查询功能查询输入的用户是否存在,若存在将返回账户的余额
     18     res = pay_check(name_inp)
     19     res1 = int(res)
     20     # print(res)
     21     while 1:
     22         money_inp = input("请输入充值金额:").strip()
     23         # 判断输入的金额是否是数字,不是则重新输入
     24         if not money_inp.isdigit():
     25             print("请输入的必须是数字".center(50, "*"))
     26             continue
     27         money_inp = int(money_inp)
     28 
     29         res1 += money_inp
     30         # dic[name_inp] += money_inp
     31         print("充值成功".center(50, "*"))
     32         # 打开文件,将文件内容读取到内存中。若数据大,则推荐使用os模块
     33         # 将新的数据写入copy文件然后重命名
     34         with open("db.txt", "r", encoding="utf8") as f:
     35             dic = {}
     36             for line in f:
     37                 user, money = line.strip().split(":")
     38                 dic[user] = int(money)
     39             if name_inp in dic:
     40                 dic[name_inp] = res1
     41             else:
     42                 return
     43         with open("db.txt", "w", encoding="utf8") as w:
     44             for user, money in dic.items():
     45                 w.write("%s:%s
    " % (user, money))
     46         break
     47 
     48 
     49 # 转账功能
     50 def transfer():
     51     A_name = input("请输入要转账的账户:").strip()
     52     res_A = pay_check(A_name)
     53     if res_A < 0:
     54         return
     55     B_name = input("请输入要接收的账户:").strip()
     56     res_B = pay_check(B_name)
     57     if res_B < 0:
     58         return
     59 
     60     money = int(input("请输入要转账的金额:").strip())
     61     res_A1 = int(res_A)
     62     res_B1 = int(res_B)
     63 
     64     if res_A1 > money:
     65         res_B1 += money
     66         res_A1 -= money
     67         print(res_A1, res_B1)
     68         print("转账成功".center(50, "*"))
     69 
     70     with open("db.txt", "r", encoding="utf8") as f:
     71         dic = {}
     72         for line in f:
     73             user, money = line.strip().split(":")
     74             dic[user] = int(money)
     75         dic[A_name] = res_A1
     76         dic[B_name] = res_B1
     77     with open("db.txt", "w", encoding="utf8") as w:
     78         for user, money in dic.items():
     79             w.write("%s:%s
    " % (user, money))
     80 
     81 
     82 # 取现功能
     83 def pay_cash():
     84     A_name = input("请输入要提现的账户:").strip()
     85     res_A = pay_check(A_name)
     86     if res_A < 0:
     87         return
     88 
     89     while 1:
     90         money = input("请输入要提现的金额:").strip()
     91         if not money.isdigit():
     92             print("输入的金额必须是数字".center(50, "*"))
     93         money = int(money)
     94 
     95         if money > res_A:
     96             print("提现金额不足...".center(50, "*"))
     97         else:
     98             res_A -= money
     99             print("取现成功".center(50, "*"))
    100 
    101         with open("db.txt", "r", encoding="utf8") as f:
    102             dic = {}
    103             for line in f:
    104                 user, money = line.strip().split(":")
    105                 dic[user] = int(money)
    106             # if A_name in dic:
    107             dic[A_name] = res_A
    108         with open("db.txt", "w", encoding="utf8") as w:
    109             for user, money in dic.items():
    110                 w.write("%s:%s
    " % (user, money))
    111         break
    112 
    113 
    114 # 查询功能
    115 def pay_check(name):
    116     dic = {}
    117     with open("db.txt", "r", encoding="utf8") as f:
    118         for line in f:
    119             user, money = line.strip().split(":")
    120             dic[user] = int(money)
    121 
    122     if name not in dic:
    123         print("用户不存在")
    124         choice = input("是否需要注册账户(Y/N):").strip()
    125         if choice.upper() == "Y":
    126             pay_register()
    127         else:
    128             print("结束本次操作".center(50, "*"))
    129             return -1
    130     else:
    131         print("账户【%s】的余额【%s】" % (name, dic[name]))
    132         return dic[name]
    133 
    134 
    135 # 注册功能
    136 def pay_register():
    137     username = input("请输入你要注册的账户:").strip()
    138     while 1:
    139         money_inp = input("请输入你要充值的金额:").strip()
    140         if not money_inp.isdigit():
    141             print("请输入数字!".center(50, "*"))
    142             continue
    143         else:
    144             print("充值成功!".center(50, "*"))
    145             money_inp = int(money_inp)
    146 
    147         with open("db.txt", "a", encoding="utf8") as f:
    148             f.write("%s:%s
    " % (username, money_inp))
    149         return
    150 
    151 
    152 # 执行代码
    153 def my_choice():
    154     msg = """
    155         "1": "充值",
    156         "2": "转账",
    157         "3": "取现",
    158         "4": "查询",
    159         "5": "注册",
    160         "6": "退出"
    161     """
    162     msg_dic = {
    163         "1": pay_money,
    164         "2": transfer,
    165         "3": pay_cash,
    166         "4": pay_check,
    167         "5": pay_register,
    168     }
    169     while 1:
    170         print(msg)
    171         choice = input("请输入你要做的操作:").strip()
    172         if choice == "6": break
    173         if choice not in msg_dic:
    174             print("输入有误,重新输入!".center(50, "*"))
    175             continue
    176         if choice == '4':  #
    177             data = input(">>>>输入账户:").strip()
    178             msg_dic[choice](data)
    179             continue
    180         msg_dic[choice]()
    181 
    182 
    183 if __name__ == "__main__":
    184     my_choice()
  • 相关阅读:
    界面间传值
    消息通知中心
    ios外部链接或者app唤起自己的app
    iOS跳转第三方应用举例一号店和京东
    ios 传递JSON串过去 前面多了个等号
    react-native 配置 在mac 上找不到.npmrc
    webView 获取内容高度不准确的原因是因为你设置了某个属性
    WKWebView 加载本地HTML随笔
    关于attibutedText输出中文字符后的英文和数字进行分开解析的问题
    iOS 企业包碰到的问题
  • 原文地址:https://www.cnblogs.com/Tang-Yuan/p/12916919.html
Copyright © 2011-2022 走看看