0. 獎勵物品存放在文件price.txt
1. 給定年齡(隨機18-60),用戶可以猜三次年齡
2. 年齡猜對,讓用戶選擇兩次獎勵
3. 用戶選擇兩次獎勵后可以退出
import random
age = random.randint(18, 60) # 隨機一個數字,18-60歲
print(age)
count = 0 # 計數
f = open('price.txt', 'r', encoding='utf8') # price.txt右下角為什么編碼,則encoding為什么編碼
price_dict = f.read()
price_dict = eval(price_dict) # type:dict # 獲取獎品字典
f.close()
price_self = dict()
while count < 3:
count += 1
inp_age = input('請輸入你想要猜的年齡:')
# 判斷是否為純數字
if not inp_age.isdigit():
print('搞事就罵你傻逼')
continue
inp_age = int(inp_age)
# 篩選年齡范圍
if inp_age > 60 or inp_age < 18:
print('好好題目,18-60歲,非誠勿擾')
continue
# 核心邏輯
if age == inp_age:
print('猜中了,請選擇你的獎品')
# 打印商品
for k, v in price_dict.items():
print(f'獎品編號:{k} {v}')
# 獲取獎品的兩次循環
for i in range(2):
price_choice = input('請輸入你需要的獎品編號:')
if not price_choice.isdigit():
print("恭喜你已經獲得一次獎品,獎品為空!並且請輸入正確的獎品編號!")
continue
price_choice = int(price_choice)
if price_choice not in price_dict:
print('你想多了吧!')
else:
price_get = price_dict[price_choice]
print(f'恭喜中獎:{price_get}')
if price_self.get(price_get):
price_self[price_get] += 1
else:
price_self[price_get] = 1
print(f'恭喜你獲得以下獎品:{price_self}')
break
elif age > inp_age:
print('猜小了')
elif age < inp_age:
print('猜大了')
continue