猜数字加强版
'''
0. (多用户)
-
登录(保存用户信息)
-
(随机生成)给定年龄,用户可以猜三次年龄(保存用户每次输入的年龄)
-
年龄猜对,让用户选择两次奖励
-
用户选择两次奖励(选择奖励是概率性的)后可以退出
-
(输出日志)
'''
from logging_config import load_my_logging_cfg #固定写法
logger = load_my_logging_cfg() # 固定写法
import random
用户信息
user_info_dict = {
'nick': '123',
'jason': '123',
'tank': '123',
}
age_count = 0 # 计算次数
age = random.randint(1, 100) # 给定1,100内的整数
print(age)
奖品信息
prize_dict = {
'0': "芭比娃娃",
'1': "变形金刚",
'2': "psp游戏机",
'3': "奥特曼",
'4': "遥控飞机",
'5': "chongqiwawa",
'6': "再来一次",
'7': "欢迎下次光临",
}
prize_msg = '''
0 芭比娃娃
1 变形金刚
2 psp游戏机
3 奥特曼
4 遥控飞机
5 chongqiwawa
6 再来一次
7 欢迎下次光临
'''
get_prize_dict = {} # 获取的奖品信息
login_count = 0
登录
while login_count < 3:
# 与用户交流
username_inp = input('请输入你的用户名:')
pwd_inp = input('请输入你的密码:')
# 验证登录
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'沙雕,你活了{age_inp}岁吗?')
continue
age_inp_int = int(age_inp)
# 核心逻辑
if age_inp_int > age:
print('猜大了')
elif age_inp_int < age:
print('猜小了')
else:
print('bingo')
logger.info(f'{username_inp} 猜对了')
prize_count = 0
while prize_count < 2:
print(f'奖品如下:{prize_msg}')
# 与用户交流
prize_inp_choice = input('请按下y开始抽奖:')
if not (prize_inp_choice == 'y')
prize_count += 1
print('傻逼,请输入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'恭喜这个比获得奖品{}')
logger.info(f'{username_inp}获得奖品:{prize}')
prize_count += 1
print(f'总共获得奖品为:{get_prize_dict}')
logger.info(f'{username_inp}总共获得奖品为{get_prize_dict}')
login_count = 999
break
age_count += 1 # 失败计数加1