zoukankan      html  css  js  c++  java
  • 3/22 周末作业

    周末作业
    编写ATM程序实现下述功能,数据来源于文件db.txt
    0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
    1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)

    def register():
        tag = True
        while tag:
            tag1 = True
            user = input('请输入账号:').strip()
            if user:
                with open('db.txt','r',encoding='utf-8') as f:
                    for i in f:
                        i, *_ = i.strip().split(':')
                        if i == user:
                            tag1 = False
                            print('该用户已存在...')
                            break
                if tag1:
                    pwd = input('请输入密码:').strip()
                    pwd1 = input('请再次输入确认密码:').strip()
                    if pwd:
                        if pwd == pwd1:
                            while tag:
                                money = input('请输入你要存的金额:').strip()
                                if money.isdigit():
                                    with open('db.txt','a',encoding='utf-8') as f5:
                                        f5.write('{}:{}:{}
    '.format(user,pwd,money))
                                    print('注册成功')
                                    tag = False
                                else:
                                    print('金额输入有误,请输入整数')
                        else:
                            print('密码不一致')
                    else:
                        print('密码不能为空哦')
            else:
                print('账号不能为空哦')
    
    
    def log_in():
        import os,time
        global login
        global user_in
        while True:
            user = input('请输入账号:').strip()
            if os.path.exists('{}'.format(user)):
                with open('{}'.format(user),'r',encoding='utf-8') as f:
                    l = f.read()
                if float(l) > time.time():
                    print('该用户被锁定了')
                    continue
                else:
                    os.remove('{}'.format(user))
            with open('db.txt','r',encoding='utf-8') as f5:
                for line in f5:
                    user1, pwd1, money1 = line.strip().split(':')
                    if user == user1:
                        count = 0
                        while count < 3:
                            pwd = input('请输入密码:').strip()
                            if pwd == pwd1:
                                login = True
                                user_in = user
                                print('登陆成功')
                                return 1
                            else:
                                count += 1
                                print('登录失败')
                        with open('{}'.format(user),'w',encoding='utf-8') as f:
                            f.write(str(time.time() + 300))
                        print('该用户已经被锁定了,傻叉')
                        login = False
                        return login
                else:
                    print('没有这个用户哦')
    

    下述操作,要求登录后才能操作
    1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改

    def money_in():
        if login:
            while True:
                money = input('请输入充值金额:').strip()
                if money.isdigit():
                    with open('db.txt','r',encoding='utf-8') as f,
                        open('.db.txt.swap','w',encoding='utf-8') as f1:
                        for line in f:
                            user1, pwd1, money1 = line.strip().split(':')
                            if user_in == user1:
                                money1 = int(money1)
                                money1 += int(money)
                            f1.write('{}:{}:{}
    '.format(user1, pwd1, money1))
                        print('充值成功,充值金额为:{}'.format(money))
    
                else:
                    print('请输入整数数字')
                    continue
                os.remove('db.txt')
                os.rename('.db.txt.swap','db.txt')
                return 1
        else:
            print('请登陆后操作')
    

    2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱

    def transfer():
        if login:
            d = {}
            user = input('你想转给谁:').strip()
            with open('db.txt','r',encoding='utf-8') as f:
                for line in f:
                    user1, pwd1, money1, = line.strip().split(':')
                    d[user1] = [pwd1,int(money1)]
            if user in d:
                money = input('转多少:').strip()
    
                if d[user_in][1] > int(money):
                    d[user_in][1] -= int(money)
                    d[user][1] += int(money)
                    print('转账成功,金额为:{}'.format(money))
                    with open('db.txt','w',encoding='utf-8') as f5:
                        for i in d:
                            f5.write('{}:{}:{}
    '.format(i,d[i][0],d[i][1]))
                    return 1
                else:
                    print('没那么多钱')
            else:
                print('没这个人哦')
            return 0
        else:
            print('请登录后操作')
    

    3、提现功能:用户输入提现金额,db.txt中该账号钱数减少

    def top_up():
        if login:
            while True:
                money = input('提现金额:').strip()
                if money.isdigit():
                    with open('db.txt','r',encoding='utf-8') as f,
                            open('.db.txt.swap','w',encoding='utf-8') as f2:
                        for i in f:
                            user1, pwd1, money1 = i.strip().split(':')
                            if user1 == user_in:
                                money1 = int(money1)
                                money = int(money)
                                if money < money1:
                                    money1 -= money
                                    print('提现成功,金额为:{}'.format(money))
                                else:
                                    print('没那么多钱')
                            f2.write('{}:{}:{}
    '.format(user1, pwd1, money1))
                    os.remove('db.txt')
                    os.rename('.db.txt.swap','db.txt')
                    return 1
                else:
                    print('请输入整数数字,傻叉')
        else:
            print('请登录后操作')
            return 0
    

    4、查询余额功能:输入账号查询余额

    def check_account():
        if login:
            with open('db.txt','r',encoding='utf-8') as f:
                for line in f:
                    user1, pwd1, money1, = line.strip().split(':')
                    if user1 == user_in:
                        print('余额为:{}'.format(money1))
                        return 1
        else:
            print('请登录后操作')
    
    if __name__ == '__main__':
        import os
        login = False
        user_in = None
        dic_func = {
            '0':['退出',exit],
            '1':['登录',log_in],
            '2':['提现',top_up],
            '3':['充值',money_in],
            '4':['转账',transfer],
            '5':['查询余额',check_account],
            '6':['注册',register]
        }
        while True:
            print('===================================')
            for i in range(0,len(dic_func)):
                print('   {} {}'.format(i,dic_func[str(i)][0]))
            print('===================================')
            choice = input('请输入命令编号:').strip()
            if choice.isdigit():
                dic_func[choice][1]()
            else:
                print('请输入命令编号')
    
  • 相关阅读:
    Redis之面试题总结
    nginx入门,安装
    NGINX工作原理(2)
    Ngnx工作原理(1)
    Linux之常用脚本
    进程管理工具之supervisor[详解]
    PHP进阶书籍
    LVS、Nginx及HAProxy
    高级程序员与初级程序员差别在哪里?
    详解Wox
  • 原文地址:https://www.cnblogs.com/pythonwl/p/12546359.html
Copyright © 2011-2022 走看看