Tiny_Lu
不忘初心

day 10 函数作业

# 输入用户名密码
def user_inp():
    user_name_inp = input('输入用户名:')
    user_pwd_inp = input('输入密码:')
    return user_name_inp, user_pwd_inp

# 注册
def regist(user_name_inp, user_pwd_inp):
    re_user_pwd_inp = input('再一次输入密码:')
    count = 0
    while count < 3:
        if user_pwd_inp == re_user_pwd_inp:
            with open('user_info.txt', 'a', encoding='utf8') as fa:
                fa.write(f'{user_name_inp}:{user_pwd_inp}\n')
                print('注册成功')
                break
        else:
            print('两次密码不一致!')
            count += 1

# 登录
def login(user_name_inp, user_pwd_inp):
    with open('user_info.txt', 'r', encoding='utf8') as fr:
        for i in fr:
            user_name, user_pwd = i.split(':')
            # print(user_name, user_pwd)
            if user_name.strip() == user_name_inp and user_pwd.strip() == user_pwd_inp:
                print('登陆成功')
                user_info['name'] = user_name
        else:
            print('用户名密码错误')




# 猜年龄
def guess():
    count = 0
    print(f"当前用户名:{user_info['name']}")
    while count < 3:
        age = 18
        print('请输入年龄,猜对有奖品哦!')

        age_inp = input('>>')
        if not age_inp.isdigit():
            print('非法输入')
            continue
        age_inp = int(age_inp)
        if age_inp > age:
            print('猜大了')
        elif age_inp < age:
            print('猜小了')
        else:
            print('猜对了')
            choose_prize()
            break
        count += 1
        print('你与奖品擦肩而过')

# 选择奖励
def choose_prize():
    prize_dict = {
        '0': "芭比娃娃",
        '1': "变形金刚",
        '2': "psp游戏机",
        '3': "奥特曼",
        '4': "遥控飞机",
        '5': "chongqiwawa",
    }
    prize_msg = '''
    0 芭比娃娃
    1 变形金刚
    2 psp游戏机
    3 奥特曼
    4 遥控飞机
    5 chongqiwawa
    '''
    prize_count = 0

    print(prize_msg)

    while prize_count < 2:
        prize_choice = input('请选择奖品:')
        if not prize_choice.isdigit():
            print('请选择正确的奖品')
            continue
        prize = prize_dict[prize_choice]
        if prize not in get_price_dict:
            get_price_dict[prize] = 1
        else:
            get_price_dict[prize] += 1

        prize_count += 1
    print(f'当前获得奖品:{get_price_dict}')



user_info = {}
get_price_dict = {}
while 1:
    print('''
    0 登录
    1 注册
    2 猜年龄
    q 退出
    ''')
    choice = input('>> ')

    if choice == '0':
        user_name_inp, user_pwd_inp = user_inp()
        login(user_name_inp, user_pwd_inp)

        print('是否猜年龄(y/n):')
        choice_guess = input('>>')
        if choice_guess == 'y':
            guess()

    elif choice == '1':
        user_name_inp, user_pwd_inp = user_inp()
        regist(user_name_inp, user_pwd_inp)
    elif choice == '2':
        if len(user_info) == 0:
            print('请先登录')
            continue
        else:
            guess()

    elif choice == 'q':
        exit()

posted @ 2019-09-19 19:54  二二二二白、  阅读(119)  评论(0编辑  收藏  举报