day10

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

  1. 登录函数
  2. 注册函数
  3. 猜年龄函数
  4. 选择奖品函数
def login():
    '''登陆'''
    count_login = 3
    while count_login > 0:
        #与用户交互
        user_name_inp = input('user_name:')
        user_pwd_inp = input('user_pwd:')
        count_login -= 1
        # 验证
        with open('users_info.txt','r',encoding='utf-8') as fr:
            for line in fr:
                user_name,user_pwd = line.strip().split(':')
                if user_name == user_name_inp and user_pwd == user_pwd_inp:
                    print('登陆成功')
                    break
            else:
                print('用户名或密码错误')
                continue
            break
def regeist():
    '''注册'''
    # 与用户交互
    count_regeist = 3
    while count_regeist > 0:
        user_name_inp = input('user_name:')
        user_pwd_inp = input('user_pwd:')
        re_pwd_inp = input('re_pwd:')
        count_regeist -= 1

        # 验证密码
        if user_pwd_inp != re_pwd_inp:
            print('两次密码不一致,请重新输入')
            continue

        # 验证用户名是否可用
        with open('users_info.txt', 'r', encoding='utf-8') as fr, \
                open('users_info_swap.txt', 'w', encoding='utf-8') as fw:
            for line in fr:
                print(line)
                user_name, user_pwd = line.strip().split(':')
                fw.write(f'{user_name}:{user_pwd}\n')
                if user_name == user_name_inp:
                    print('用户名已被注册,请重新选择')
                    break
            else:
                print('注册成功')
                fw.write(f'{user_name_inp}:{user_pwd_inp}\n')
                break
    import os
    os.remove(r'uers_info.txt')
    os.rename(r'uers_info_swap.txt', r'uers_info.txt')
                
def select_prize():
    '''选择奖品'''
    prize_dic = {'1': 'A', '2': 'B', '3': 'C', '4': 'D', '5': 'E', '6': 'F'}
    #  打印奖品列表
    print('奖品如下:')
    for prize_num, prize_name in prize_dic.items():
        print(prize_num, prize_name)


    # 开始选择
    prize_select_dic = {}
    count_select = 2
    while count_select > 0:
        # 与用户交互
        prize_num_inp = input('prize_num:')
        count_select -= 1
        if prize_num_inp not in prize_dic:
            continue
        # 加入到已选列表
        prize_select = prize_dic[prize_num_inp]
        if prize_select not in prize_select_dic:
            prize_select_dic[prize_select] = 1
        else:
            prize_select_dic[prize_select] += 1
        print(f'恭喜您获得奖品{prize_select}')

    print(f'恭喜你共获得奖品{prize_select_dic}')
    
    
def guess_age():
    '''猜年龄游戏'''
    # 定义人物信息,随机猜测某个人的年龄
    import random
    name_dic = {'alex': 18, 'nick': 19, 'bzr': 3}
    name_guess = random.choice(list(name_dic.keys()))
    age = name_dic[name_guess]

    print(f'猜猜{name_guess}的年龄时多少?')
    count_guess = 3
    while count_guess > 0:
        # 与用户交互
        age_inp = input('age:')
        count_guess -= 1
        if not age_inp.isdigit():
            print(f'你的年龄是{age_inp}吗?请重新输入')
            continue
        # 判断是否正确
        age_inp = int(age_inp)
        if age_inp > age:
            print('猜大了')
        elif age_inp < age:
            print('猜小了')
        else:
            print('猜对了,获得两次选择奖品的机会')
            select_prize()
            break
print('''
1:登陆,
2:注册,
3:猜年龄游戏
q:退出
''')
while True:
    choice = input('选择:1~3或者q:')
    if choice == '1':
        login()
    elif choice == '2':
        regeist()
    elif choice == '3':
        guess_age()
    elif choice == 'q':
        break
posted @ 2019-09-19 17:32  W文敏W  阅读(122)  评论(0编辑  收藏  举报