day10 作业

猜年龄升级版


'''
1. 可以直接玩猜年龄游戏,不需要登录
2. 登录成功后玩猜年龄游戏
3. 猜年龄猜中后,可以选择两次奖品
4. 注册的用户名不能重复注册
'''
import random

prize_dict = {
    '0': '奥特曼',
    '1': '钢铁侠',
    '2': '《笨方法学python》',
    '3': '泰国一日游',
    '4': 'iphoneX',
    '5': '娃娃',
    '6': '阿拉丁',
    '7': '特斯拉',
    '8': '谢谢惠顾'
}

prize_cart = dict()  # 最后把奖品放在这里


def register():
    """注册功能"""
    print('欢迎来到注册界面!')

    count = 0
    while count < 3:
        # 与用户交互
        username_inp = input('请输入你的用户名:').strip()
        pwd_inp = input('请输入你的密码:').strip()
        re_pwd_inp = input('请再次确认密码:').strip()

        # 判断两次密码输入是否一致
        if re_pwd_inp != pwd_inp:
            print('两次输入密码不一致')
            count += 1
            continue

        # 注册到文件中,判断用户名是否已经注册过
        with open('userinfo.txt', 'a', encoding='utf-8') as fa:
            with open('userinfo.txt', 'r', encoding='utf-8') as fr:
                data = fr.read()
                if username_inp in data:
                    print(f'用户名{username_inp}已经存在,请重新注册')
                    count += 1
                    continue
            fa.write(f'{username_inp}:{pwd_inp}
')
            print('注册成功')
            break


def login():
    """登录功能"""
    print('请登录。。。')
    count = 0
    while count < 3:
        # 与用户交互
        username_inp = input('请输入你的用户名:').strip()
        pwd_inp = input('请输入你的密码:').strip()

        # 验证用户信息
        with open('userinfo.txt', 'r', encoding='utf-8') as fr:
            for i in fr:
                i = i.strip('
')
                username, pwd = i.split(':')

                if username_inp == username and pwd_inp == pwd:
                    print('登录成功')
                    count = 3
                    return guess_age()


            else:
                print('账号或密码错误')
                count += 1


def select_prize():
    """选择奖品功能"""

    # 打印奖品
    print('奖品信息如下:
')
    for i, j in prize_dict.items():
        print(f'奖品编号:{i} {j}')

    for i in range(2):  # 可以选择两次奖品
        prize_choice = input('请选择你想要奖品的编号,如果不想要按q退出').strip()
        if prize_choice == 'q':
            break

        if prize_choice not in prize_dict:
            print('没有这个奖品')
        else:
            prize_get = prize_dict[prize_choice]
            print(f'恭喜你获得奖品:{prize_get}')

            # 把奖品加入
            if prize_get in prize_cart:
                prize_cart[prize_get] += 1
            else:
                prize_cart[prize_get] = 1

    print(f'恭喜你获得以下奖品:{prize_cart}')


def guess_age():
    """猜年龄功能"""
    print('欢迎来玩猜年龄游戏, 范围为18-80')
    age = random.randint(18, 80)
    print(f'随机年龄:{age}')

    count = 0
    while count < 3:
        count += 1
        age_inp = input('请输入你的年龄:').strip()

        if not age_inp.isdigit():  # 判断是否输入的是数字
            print('傻逼,你不知道要输入数字吗')
            continue

        age_inp = int(age_inp)

        # 删选年龄范围
        if age_inp > 80 or age_inp < 18:
            print('超出年龄范围了')
            continue

        # 核心代码
        if age_inp == age:
            print('猜对了')
            return select_prize()

        elif age_inp < age:
            print('猜小了')

        else:
            print('猜大了')

        continue


func_msg = '''
1. 注册
2. 登录
3. 猜年龄游戏
'''

func_dict = {
    '1': register,
    '2': login,
    '3': guess_age
}

print(func_msg)

# 撸棒性
while True:
    choice = input('请选择功能:').strip()
    if not choice.isdigit():
        print('非法字符,请重新输入')
        continue
    elif int(choice) > len(func_dict.keys()):
        print('超出范围,没有这个功能')
        continue
    else:
        break

func_dict.get(choice)()
原文地址:https://www.cnblogs.com/setcreed/p/11552046.html