day 10作业

目录

  • 1.注册函数
  • 2.登录函数
  • 3.猜年龄
  • 4.奖品选择

1.注册函数

def zc():
    """注册"""
    count = 0
    while count < 3:
        id_inp = input('请输入用户名:')
        pwd_inp = input('请输入密码:')
        re_pwd_inp = input('请确认密码:')
        if not pwd_inp == re_pwd_inp:
            print('密码不一致')
            continue
        else:
            with open('test.txt', 'a', encoding='utf8') as fa:
                fa.write(f'{id_inp}:{pwd_inp}\n')
                fa.flush()
            break
        count += 1

2.登录函数

def dl():
    """登录"""
    id_inp = input('请输入用户名:')
    pwd_inp = input('请输入密码:')
    with open('test.txt', 'r', encoding='utf8') as fr:
        for user_info in fr:
            username, password = user_info.split(':')
            if id_inp == username.strip() and pwd_inp == password.strip():
                print('登录成功')
                break
        else:
            print('登录失败')

3.猜年龄

def cnl():
    """猜年龄游戏"""
    import random

    age = random.randint(0, 100)
    count = 0
    while count < 3:
        age_inp = input('请输入年龄:')
        if not age_inp.isdigit():
            print('格式错误!')
            continue
        age_inp_int = int(age_inp)
        if age_inp_int < age:
            print('猜小了')
        elif age_inp_int > age:
            print('猜大了')
        else:
            print('猜对了')
            gift_get()
            count = 3
        count += 1
        if count == 3:
            again_inp = input('是否需要再次玩耍?(Y/N)')
            if again_inp == 'Y' or again_inp == 'y':
                count = 0

4.奖品选择

def gift_get():
    """奖品选择"""
    count = 0
    gift_dic = {
        '0': '小猪佩奇',
        '1': '懒羊羊',
        '2': '喜羊羊',
        '3': '小黄鸭',
        '4': '仙女棒'
    }
    gift_list = '''
      0:小猪佩奇
      1: 懒羊羊
      2: 喜羊羊
      3: 小黄鸭
      4: 仙女棒
      '''
    gift_all = {}

    while count < 2:
        print(f'请选择奖品:\n{gift_list}')
        gift_inp = input('请输入对应序号领取奖品:')
        if gift_inp in gift_dic:
            gift = gift_dic[gift_inp]
            if gift in gift_all:
                gift_all[gift] += 1
            else:
                gift_all[gift] = 1

            count += 1
        else:
            break
        print(f'您的第{count}件奖品为:{gift}')
    print(f'您共获得\n{gift_all}')

你可能感兴趣的:(day 10作业)