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()

转载于:https://www.cnblogs.com/2222bai/p/11552180.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值