课后作业 day10

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

# 2. 登录函数
# 3. 注册函数
# 4. 猜年龄函数
# 5. 选择奖品函数
import random

def register():
    '''注册功能'''
    count = 0
    while count < 3 :
        username = input('请输入用户名:')
        password = input('请输入密码:')
        password_confirm = input('请确认密码:')

        if not password == password_confirm:
            print('两次输入的密码不一致!')
            count += 1
            continue
        with open('user_info.txt', 'a', encoding='utf8') as fa:
            fa.write(f'{username}:{password}\n')
            fa.flush()
            print('注册成功!')
            break
    else:
        print('别捣乱!!')

def login():
    '''登录功能'''
    count_login = 0
    while count_login < 3:
        get_username = input('请输入用户名:')
        get_password = input('请输入密码:')
        with open('user_info.txt', 'r', encoding='utf8') as fr:
            ueser_data = f'{get_username}:{get_password}'
            print(ueser_data)
            for i in fr:
                print(i)
                if not ueser_data == i.strip():
                    continue
                else:
                    print('登陆成功!')
                    break
            else:
                print('用户名密码错误!')
                count_login += 1
    print('用户名密码输入错误超过三次!')

def guess():
    '''猜年龄'''
    count_age = 0
    age = int(random.random()*100)
    print(age)
    while count_age < 3:
        get_age = int(input('请输入年龄:'))

        if get_age > age:
            print('猜大了!')
            count_age += 1
        elif get_age < age:
            print('猜小了')
            count_age += 1
        else:
            print('猜对了!')
            break
    else:
        print('猜错次数过多!')

def prize():
    '''选择奖品'''
    prize_dic = {"0": "ipad", "1": "iphone", "2": "ipod"}
    prize_msg = '''"0":"ipad"
    "1":"iphone"
    "2":"ipod"
    '''
    count_choice = 0
    while count_choice < 3:
        print(prize_msg)
        get_choice = input('请输入您的奖品:')
        if get_choice in prize_dic:
            print(f'恭喜您获得{prize_dic[get_choice]}!!')
            break
        else:
            print('请输入正确的奖品')
            count_choice += 1
    print('奖品输入错误次数过多!')

register()
login()
guess()
prize()
posted @ 2019-09-19 16:58  simple123  阅读(155)  评论(0编辑  收藏  举报