zoukankan      html  css  js  c++  java
  • day16 本日作业+周末作业

    1.编写计数器功能,要求调用一次在原有的基础上加1

    def func():
        x=0
        def counter():
            nonlocal x
            x+=1
            return x
        return counter
    counter = func()
    print(counter())
    print(counter())
    print(counter())
    print(counter())
    print(counter())
    

    2.周末作业

    编写ATM程序实现下述功能,数据来源于文件db.txt
    0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
    1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
    下述操作,要求登录后才能操作
    1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
    2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
    3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
    4、查询余额功能:输入账号查询余额

    import os
    def recharge():
        ''' 充值功能'''
        name_inp = input("请输入要充值的账号>")
        money_inp = input("请输入要充值的金钱>")
        with open("db.txt","r",encoding="utf-8") as f ,
            open(".db.txt.swap","w",encoding="utf-8") as f1:
                for line in f :
                    if name_inp in line :
                        name,pwd,money = line.strip().split(":")
                        new_money = int(money_inp)+int(money)
                        f1.write(line.replace(money,str(new_money)))
                    else:
                        f1.write(line)
        os.remove("db.txt")
        os.rename(".db.txt.swap","db.txt")
    
    def transfer():
        '''转账功能'''
        transfer_name_inp = input("请输入转账人姓名:")
        collector_name_inp = input("请输入收账人姓名:")
        money_inp = input("请输入转账金额")
        with open("db.txt","r",encoding="utf-8") as f ,
            open(".db.txt.swap","w",encoding="utf-8") as f1:
            for line in f :
                if transfer_name_inp in line :
                    transfer_name,pwd,transfer_money=line.strip().split(":")
                    new_money = int(transfer_money)-int(money_inp)
                    f1.write(line.replace(transfer_money,str(new_money)))
                elif collector_name_inp in line:
                    collector_name,pwd,collector_money=line.strip().split(":")
                    new_money = int(collector_money)+int(money_inp)
                    f1.write(line.replace(collector_money,str(new_money)))
                else:
                    f1.write(line)
        os.remove("db.txt")
        os.rename(".db.txt.swap","db.txt")
    def cash_withdrawal():
        '''提现功能'''
        name_inp = input("请输入提现账号的姓名:")
        money_inp = input("请输入提现的金额:")
        with open("db.txt","r",encoding="utf-8") as f ,
            open(".db.txt.swap","w",encoding="utf-8") as f1:
            for line in f:
                if name_inp in line:
                    name,pwd,money = line.strip().split(":")
                    new_money = int(money)-int(money_inp)
                    f1.write(line.replace(money,str(new_money)))
                else:
                    f1.write(line)
        os.remove("db.txt")
        os.rename(".db.txt.swap","db.txt")
    
    def query():
        '''查询功能'''
        name_inp=input("请输入你要查询的用户名:")
        with open("db.txt","r",encoding="utf-8")as f :
            for line in f :
                name,pwd,money = line.strip().split(":")
                if name_inp in line:
                    print(money)
                    break
    
    def register():
        '''注册'''
        username = input("请输入注册的用户名:")
        with open("db.txt","r",encoding="utf-8") as f:
            res = f.read()
            if username in res :
                print("该账号已存在,注册失败")
            else:
                password1 = input("请输入注册的密码:")
                password2 = input("请再输一次密码:")
                money = input("请输入你的初始金额:")
                if password1 == password2 :
                    with open("db.txt","a",encoding="utf-8") as f1 :
                        f1.write(f"{username}:{password1}:{money}
    ")
                else:
                    print("两次输入的密码不同,注册失败")
    def login():
        '''登录功能'''
        list1=[]
        while True:
            user_inp = input("your name >")
            if os.path.exists(f"locked/{user_inp}"):
                print("该账户被锁定")
            else:
                pwd_inp = input("your msg >")
                with open("db.txt","r",encoding="utf-8") as f:
                    for line in f :
                        username,password,money = line.strip().split(":")
                        if username==user_inp and pwd_inp == password:
                            print("登录成功")
                            return 1
                    else:
                        if list1.count(user_inp)==2:
                            with open(f"locked/{user_inp}","w",encoding="utf-8") as f:
                                pass
                            print("锁定账号")
                        else:
                            list1.append(user_inp)
                            print(f"该账号已经输错{list1.count(user_inp)}次")
    #主程序
    dict0={
        "0":[exit,"退出"],
        "1":[register,"注册"],
        "2":[login,"登录"],
    }
    dict1={
         0:[None,"退出"],
        "1":[recharge,"充值"],
        "2":[transfer,"转账"],
        "3":[cash_withdrawal,"提现"],
        "4":[query,"查询"]
    }
    def order(dic):
        '''判断指令'''
        while True:
            for i in dic:
                print(f"{i}:{dic[i][1]}")
            cmd =input("请输入指令>")
            if not cmd.isdigit():
                print("请输入数字")
            elif cmd in dic:
                res = dic[cmd][0]()
                return res
            elif cmd == "0":
                print("退出")
                break
            else:
                print("请输入正确指令")
    while True:
        res = 0
        res = order(dict0)
        if res:
            order(dict1)
    
    
    
  • 相关阅读:
    canvas gamebricks
    javascript 判断对象的值是否相等
    让控件全屏显示(WinForm)
    网络视频监控
    回调函数
    [导入][链接]FreeBSD + Sun Java
    [导入][一点一滴学英语]20060330
    [导入][来自网络] 酸酸甜甜就是我 不用我多介绍了 可是你是否听过天津话的版本?
    [导入][链接]差点漏掉 Eclipse 3.2 M6出来了
    [导入][链接]Five reasons why you should never use PostgreSQL
  • 原文地址:https://www.cnblogs.com/hz2lxt/p/12532650.html
Copyright © 2011-2022 走看看