Guess Age game expansion

'''

  1. (Multi-user)

  2. Login (Save User Information)

  3. (Randomly generated) of a given age, a user can guess (ages save each user input) three times the age of

  4. Age guessed, allowing users to select the award twice

  5. After the user selects the award twice (select bonus is probabilistic) exit

  6. (Output log)
    ''

from logging_config import load_my_logging_cfg # fixed wording

logger = load_my_logging_cfg () # fixed wording

import random

User Info

= {user_info_dict
'Nick': '123',
'Jason': '123',
'Tank': '123',
}
age_count # = 0 the number of calculations
age = random.randint (1, 100) # Age given (1 integer in the range -100)
Print (Age)

Prizes Information

= {prize_dict
'0': "Barbie",
'1': "Transformers",
'2': "PSP consoles',
'3':" Altman ",
" 4 ":" remote control aircraft "
'. 5': "chongqiwawa ',
'. 6": "again",
'7': "always welcome"
}
prize_msg = '' '
0 Barbie
1 Transformers
2 psp game machine
3 Altman
4 remote aircraft
5 chongqiwawa
6 again
7 always welcome
'' '
get_prize_dict = {} # get prizes information
login_count = 0

log in

login_count the while <3:
# interacts with the user
username_inp = input ( 'Please enter your user name:')
pwd_inp the INPUT = ( 'Please enter your password:')

# 验证登录
if not (username_inp in user_info_dict and pwd_inp == user_info_dict.get(username_inp)):
    login_count += 1

    print('账号密码错误')
    continue

print('登录成功,开始游戏!\n')

# 猜年龄游戏
while age_count < 3:

    age_inp = input('请输入你的年龄:')  # 与用户交互
    logger.info(f'用户 {username_inp} 第 {age_count} 次输入年龄为 {age_inp}')

    # robust(健壮性/撸棒性)
    if not age_inp.isdigit():
        print(f'\033[1;31;40m傻吊,你活了{age_inp}岁吗?\033[0m')  # f格式化可以拼接数字和字符串
        continue

    age_inp_int = int(age_inp)

    # 核心逻辑
    if age_inp_int > age:
        print('\033[1;31;40m猜大了\033[0m')
    elif age_inp_int < age:
        print('\033[1;31;40m猜小了\033[0m')
    else:
        print('猜对了')

        logger.info(f'{username_inp} 用户猜对了')

        prize_count = 0
        while prize_count < 2:
            print(f'\033[1;33;m奖品如下:{prize_msg}\033[0m')

            # 与用户交互
            prize_inp_choice = input('请按下Y/y开始抽奖:')

            if not (prize_inp_choice == 'Y' or prize_inp_choice == 'y'):
                prize_count += 1

                print('傻逼,请输入Y/y')
                continue

            prize_choice = random.randint(0, 15)

            # 调概率
            if prize_choice in [6, 8]:
                prize_choice = 6
                prize_count -= 1
            elif prize_choice in [9, 10, 11, 12, 13, 14, 15]:
                prize_choice = 7
            prize_choice = str(prize_choice)

            prize = prize_dict[prize_choice]

            # 奖品信息放入购物车
            if prize in get_prize_dict:
                get_prize_dict[prize] += 1
            else:
                get_prize_dict[prize] = 1

            print(f'恭喜你获得奖品 \033[1;31;m{prize}\033[0m')
            logger.info(f'{username_inp}获得奖品 \033[1;31;m{prize}\033[0m')

            prize_count += 1

        print(f'总共获得奖品为:\033[1;31;m{get_prize_dict}\033[0m')
        logger.info(f'{username_inp}总共获得奖品为:\033[1;31;m{get_prize_dict}\033[0m')
        login_count = 999
        break

    age_count += 1  # 失败计数加1

Guess you like

Origin www.cnblogs.com/shaozheng/p/11530098.html