day10work

作业

  1. 在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如

    1. 登录函数
    2. 注册函数
    3. 猜年龄函数
    4. 选择奖品函数
    import os
    def r_info(name):
        while True:
            if os.path.exists('uesr_info.txt'):
                with open('user_info.txt','r',encoding='utf-8')as fr:
                    for line in fr.readlines():
                        name_get, pwd_get = line.split(':')
                        if name_get == name:
                            return [name_get, pwd_get.strip()]
            else:
                with open('user_info.txt', 'a', encoding='utf-8')as fw:
                    pass
                break
    
    
    def w_info(name, pwd):
        with open('user_info.txt', 'a', encoding='utf-8')as fw:
            fw.write(f'{name}:{pwd}')
            fw.write('\n')
    
    
    def login():
        count = 0
        while count < 3:
            name_inp = input('请输入用户名: ').strip()
            pwd_inp = input('请输入密码: ').strip()
            info = r_info(name_inp)
            if info:
                if info[0] == name_inp and info[1] == pwd_inp:
                    user_session.append(name_inp)
                    print('登陆成功!')
                    break
                else:
                    print(555)
            else:
                print('用户不存在!')
                count += 1
    
    def register():
        count1 = 0
        while count1 < 3:
            name = input('请输入用户名: ').strip()
            pwd = input('请输入密码: ').strip()
            re_pwd = input('请再次输入密码: ').strip()
            if re_pwd == pwd:
                info = r_info(name)
                if info:
                    print('用户已存在!')
                    count1 += 1
                else:
                    w_info(name, pwd)
                    print('注册成功!')
                    break
            else:
                print('两次密码不一致!')
                count1 += 1
    
    
    def guess_age():
        age = 18
        count = 0
        while count < 3:
            guess = input('请输入猜测的年龄: ').strip()
            guess = int(guess)
            if guess > age:
                print('猜大了')
            elif guess < age:
                print('猜小了')
            else:
                print('猜对了')
                choise_prize()
                break
            count += 1
    def choise_prize():
        count2 = 0
        prize_msg = '''
        0:充气娃娃
        1:女朋友
        2:男朋友
        3:AD钙奶
        4:iphone11
        '''
        prize_dict = {
            '0': '充气娃娃',
            '1': '女朋友',
            '2': '男朋友',
            '3': 'AD钙奶',
            '4': 'iphone11'
        }
        prize_list = {}
        while count2 < 2:
            print(prize_msg)
            prize_choice = input('请输入想要的奖品序号: ').strip()
            if prize_choice not in prize_dict:
                print('沙雕,没这个东西!')
                continue
            if not prize_choice.isdigit():
                print('沙雕,输数字!')
                continue
            if prize_dict[prize_choice] in prize_list:
                prize_list[prize_dict[prize_choice]] += 1
            else:
                prize_list[prize_dict[prize_choice]] = 1
            print(f'获得{prize_dict[prize_choice]}')
            count2 += 1
        print(f'你获得的奖品:{prize_list}')
    
    count, count1 = 0, 0
    tag = True
    user_session = []
    while tag:
        print('''
        1:登陆
        2:注册
        3:猜年龄
        4:退出
        ''')
    #    func_dic = {'1':login, '2': register, '3':guess_age}
        choice = input('请输入功能选择: ').strip()
        if choice == '1':
            login()
        elif choice == '2':
            register()
        elif choice == '3':
            if not user_session:
                print('请先登陆!')
            else:
                guess_age()
        elif choice == '4':
            break
        else:
            print('请输入1-3!')
    

posted on 2019-09-19 16:41  shenblogs  阅读(237)  评论(2)    收藏  举报

导航