Fork me on GitHub

day-10 作业

day10 作业

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

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

实现代码如下:

import random
import time

loggin_state = 0  # 登录状态:0:未登录;1:登录
guess_age_state = 0  # 游戏状态:0:游戏失败;1:游戏成功

# 奖品池
prize_dic = {'0': '棒棒糖', '1': '酸奶', '2': '口香糖'}
user_prize_dic = {}

# 奖品信息
prize_msg = '''
0 棒棒糖
1 酸奶
2 口香糖
'''


def loading():
    '''延时'''
    print('load', end='')
    for i in range(3):
        print('.', end='')
        time.sleep(0.3)


def registe():
    '''注册功能'''
    count = 3
    while count:
        user_name_inp = input('请输入用户名:')
        user_pwd_inp = input('请输入密码:')
        re_user_pwd_inp = input('请确认密码:')

        if not user_pwd_inp == re_user_pwd_inp:
            count -= 1
            if count != 0:
                print('两次输入的密码不一致,请重新注册!')
            else:
                print('请求次数过于频繁,请稍候再尝试。')
        else:
            with open('user_info.txt', 'a', encoding='utf-8')as fa:
                fa.write(f'{user_name_inp}:{user_pwd_inp}\n')
                loading()
                print('\n')
                print('注册成功!'.center(50, '*'))
                break


def loggin():
    '''登录功能'''
    user_name_inp = input('请输入用户名:')
    user_pwd_inp = input('请输入密码:')

    with open('user_info.txt', 'r', encoding='utf-8')as fr:
        for user_info in fr:
            user_name, user_pwd = user_info.split(':')

            if user_name_inp == user_name.strip() and user_pwd_inp == user_pwd.strip():
                loading()
                print('\n')
                print('登录成功!'.center(50, '*'))
                loggin_state = 1
                return loggin_state
        else:
            print('登录失败>_<!!!')
            print('请核对用户名密码或先进行注册~')


def guess_age():
    '''游戏功能'''
    age = random.randint(1, 101)
    print(f'只有三次机会哦,答案是{age}.')
    guess_count = 3
    while guess_count:

        user_inp = input('请输入你猜的年龄:')

        if not user_inp.isdigit():
            print('请输入纯数字!')
            continue

        user_guess_age = int(user_inp)

        if user_guess_age > age:
            print('猜大了>_<!!!')
        elif user_guess_age < age:
            print('猜小了>_<!!!')
        else:
            print('猜对了^_^~')
            guess_age_state = 1
            return guess_age_state
        guess_count -= 1


def choice_prize():
    '''选择奖品功能'''
    prize_count = 2
    while prize_count:
        print(prize_msg)
        user_choice = input('请选择你想要的奖品序号:')
        choice_num = int(user_choice)
        if choice_num not in [0, 1, 2]:
            print('!!!请输入正确的奖品序号!!!')
            continue
        prize = prize_dic[user_choice]

        if prize in user_prize_dic:
            user_prize_dic[prize] += 1
        else:
            user_prize_dic[prize] = 1
        print(f'恭喜你得到了{prize}。')
        prize_count -= 1

        if prize_count == 0:
            loading()
            print('\n')
            print(f'你一共获得了{user_prize_dic} ^_^')


# 主程序
while True:
    print('主菜单界面'.center(50, '*'))
    print('''  
    1 注册
    2 登录
    3 开始游戏
    ''')
    print('主菜单界面'.center(50, '*'))
    user_inp = input('请选择功能:')

    # 判断用户输入信息
    if not user_inp.isdigit():
        print('请输入正确的功能序号!'.center(50, '*'))
        continue

    menu_choice = int(user_inp)

    if menu_choice not in [1, 2, 3]:
        print('请输入正确的功能序号!'.center(50, '*'))
        continue

    # 账号注册
    if menu_choice == 1:
        print('账号注册界面'.center(50, '*'))
        registe()
        continue

    # 账号登录
    elif menu_choice == 2:
        print('用户登录界面'.center(50, '*'))
        loggin_state = loggin()
        continue
    # 开始游戏
    elif menu_choice == 3:
        if loggin_state == 0:
            print('请先登录账号再进行游戏~~~')
            continue

        print('游戏即将开始'.center(50, '*'))
        loading()
        print('\n')

        print('游戏界面'.center(50, '*'))
        guess_age_state = guess_age()
        print('游戏界面'.center(50, '*'))

        # 如果猜对了,选择奖品
        if guess_age_state == 1:
            print('奖品选择界面'.center(50, '*'))
            choice_prize()
            print('奖品选择界面'.center(50, '*'))

        print('游戏结束'.center(50, '*'))
        break
    else:
        print('请按照提示正确选择功能!!!'.center(50, '*'))
posted @ 2019-09-19 20:27  Yugaliii  阅读(82)  评论(0编辑  收藏  举报