• 作业3月20号


    1、函数对象优化多分支if的代码练熟

    def login():
        print('登录')
    def transfer():
        print('转账')
    def check_banlance():
        print('查询余额')
    def withdraw():
        print('提现')
    def regisster():
        print('注册')
    fanc_dic = {
        '0':['退出',None],
        '1':['登录',login],
        '2':['转账',transfer],
        '3':['查询余额',check_banlance],
        '4':['提现', withdraw],
        '5':['注册', regisster]
    }
    
    while 1:
        for k in fanc_dic:
            print(k,fanc_dic[k][0])
        choice = input('请输入命令编码:').strip()
        if not choice.isdigit():
            print('请重新输入')
            continue
        elif choice == '0':
            break
        if choice in fanc_dic:
            print(fanc_dic[choice][1]())
        else:
            print('指令不存在')


    2、编写计数器功能,要求调用一次在原有的基础上加一
    温馨提示:
    I:需要用到的知识点:闭包函数+nonlocal
    II:核心功能如下:
    def counter():
    x+=1
    return x


    要求最终效果类似
    print(couter()) # 1
    print(couter()) # 2
    print(couter()) # 3
    print(couter()) # 4
    print(couter()) # 5

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


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

    下述操作,要求登录后才能操作
    # 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
    # 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
    # 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
    # 4、查询余额功能:输入账号查询余额

    # ====================周末作业====================
    # 编写ATM程序实现下述功能,数据来源于文件db.txt
    # 0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
    '''
    def user():
        username = input('请输入用户名称: ').strip()
        password = input('请输入用户密码: ').strip()
        r_password = input('请确认用户密码: ').strip()
        if password == r_password:
            print(f'[{username}]注册成功')
            count=input('请输入存款金额:')
            with open('db.txt',mode='at',encoding='utf-8') as f1:
                f1.write('{}:{}:{}'.format(username,password,count))
        else:
            print('注册失败!')
    
    user()
    '''
    # 1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
    '''
    dic={}
    with open('db.txt', mode='rt', encoding='utf-8') as f2:
        for line in f2:
            user,word=line.strip().split(':')
            dic[user]=word
    print(dic)
    login_user=None
    def users():
        username = input('请输入用户名称: ').strip()
        if username in dic:
            password = input('请输入用户密码: ').strip()
            if password == dic[username]:
                print('登录成功!')
                global login_user
                login_user = username
        else:
            print('用户名不存在,请先注册')
            count = 0
            while count < 3:
                usernames = input('请输入用户名称: ').strip()
                password = input('请输入用户密码: ').strip()
                r_password = input('请确认用户密码: ').strip()
                if password == r_password:
                    print(f'[{usernames}]注册成功')
                    count = input('请输入存款金额:')
                    with open('db.txt', mode='at', encoding='utf-8') as f1:
                        f1.write('{}:{}:{}'.format(usernames, password, count))
                else:
                    print('注册失败!')
                    count += 1
                    continue
    
    users()
    '''
    # 下述操作,要求登录后才能操作
    # 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
    # 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
    # 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
    # 4、查询余额功能:输入账号查询余额
    dic={}
    with open('dbs.txt', mode='rt', encoding='utf-8') as f2:
        for line in f2:
            user,word=line.strip().split(':')
            dic[user]=word
    print(dic)
    login_user=None
    def users():
        username = input('请输入用户名称: ').strip()
        if username in dic:
            password = input('请输入用户密码: ').strip()
            if password == dic[username]:
                print('登录成功!')
                global login_user
                login_user = username
                while True:
                    dics = {}
                    with open('db.txt', mode='rt', encoding='utf-8') as f3:
                        for line2 in f3:
                            user2, money = line2.strip().split(':')
                            dics[user2] = int(money)
                    def transfer():
                        print('转账')
                        while True:
                            usernameb = input('请输入对方账户名称:')
    
                            if usernameb == username:
                                print('您不能给自己转账')
                                continue
                            if usernameb not in dics:
                                print('账户不存在')
                                continue
                            else:
                                outcount = input('请输入转账金额:')
                                outcount = int(outcount)
                                user3 = username
                                dics[user3] -= outcount
                                user4 = usernameb
                                dics[user4] += outcount
                                with open('db.txt', mode='wt', encoding='utf-8') as f4:
                                    for user2, money in dics.items():
                                        f4.write(f'{user2}:{money}
    ')
                                break
                    def check_banlance():
                        print('查询余额')
                        user2 = username
                        money = dics[user2]
                        print(money)
                    def withdraw():
                        print('提现')
                        incount = input('请输入提现金额:').strip()
                        incount = int(incount)
                        user2 = username
                        dics[user2] -= incount
                        with open('db.txt', mode='wt', encoding='utf-8') as f6:
                            for user2, money in dics.items():
                                f6.write(f'{user2}:{money}
    ')
                    def recharge():
                        print('充值')
                        incount = input('请输入充值金额:').strip()
                        incount = int(incount)
                        user2 = username
                        dics[user2] += incount
                        with open('db.txt', mode='wt', encoding='utf-8') as f5:
                            for user2, money in dics.items():
                                f5.write(f'{user2}:{money}
    ')
                    while True:
                        func_dic = {
                            '1': ['退出', None],
                            '2': ['转账', transfer],
                            '3': ['查询余额', check_banlance],
                            '4': ['提现', withdraw],
                            '5': ['充值', recharge]
                        }
                        for k in func_dic:
                            print(k, func_dic[k][0])
                        cmd = input('请输入命令编号(纯数字):').strip()
                        if not cmd.isdigit():
                            print('必须输入纯数字!')
                            continue
                        elif cmd == '1':
                            break
                        elif cmd in func_dic:
                            func_dic[cmd][1]()
                        else:
                            print('输入的指令不存在!')
        else:
            print('用户名不存在,请先注册')
            count = 0
            while count < 3:
                usernames = input('请输入用户名称: ').strip()
                password = input('请输入用户密码: ').strip()
                r_password = input('请确认用户密码: ').strip()
                if password == r_password:
                    print(f'[{usernames}]注册成功')
                    with open('dbs.txt', mode='at', encoding='utf-8') as f1:
                        f1.write('{}:{}
    '.format(usernames, password))
                else:
                    print('注册失败!')
                    count += 1
                    continue
    users()
  • 相关阅读:
    Unity 2018 version class.jar path
    GitHub git
    QT install
    Android Studio 安装及汉化
    Unity 调用android
    JAVA 调用c++ 扩展 批评那些垃圾,
    JAVA java调用C++动态链接库dll,有详细过程。VS2015+Eclipse以及失败解决方案
    前端将数据转化为弹幕效果的实现方式
    css圣杯布局的实现方式
    浏览器缓存问题原理以及解决方案
  • 原文地址:https://www.cnblogs.com/jingpeng/p/12535268.html
Copyright © 2020-2023  润新知