一、Python基礎實戰之猜年齡游戲
-
給定年齡,用戶可以猜三次年齡
-
年齡猜對,讓用戶選擇兩次獎勵
-
用戶選擇兩次獎勵后可以退出
age = 18 # 答案
count = 0 # 游戲次數控制
prize_dict = {0: '布娃娃', 1: '變形金剛', 2: '奧特曼', 3: '<Python從入門到放棄>'}
# 核心代碼
while count < 3:
inp_age = input('請輸入你的年齡>>>') # 與用戶交互
# 判斷用戶是否騷擾(超綱:判斷用戶輸入的是否為數字)
if not inp_age.isdigit():
print('傻逼,你的年齡輸錯了')
continue
inp_age_int = int(inp_age)
# 核心邏輯,判斷年齡
if inp_age_int == age:
print('猜對了')
print(prize_dict) # 打印獎品
# 獲取兩次獎品
for i in range(2):
prize_choice = input(
'請輸入你想要的獎品,如果不想要,則輸入"n"退出!!!') # 與用戶交互獲取獎品
# 判斷是否需要獎品
if prize_choice != 'n':
print(f'恭喜你獲得獎品: {prize_dict[int(prize_choice)]}')
else:
break
break
elif inp_age_int < age:
print('猜小了')
else:
print('猜大了')
count += 1 # 成功玩一次游戲
if count != 3:
continue
again_choice = input('是否繼續游戲,繼續請輸入"Y",否則任意鍵直接退出.') # 交互是否再一次
# 判斷是否繼續
if again_choice == 'Y':
count = 0
請輸入你的年齡>>>18
猜對了
{0: '布娃娃', 1: '變形金剛', 2: '奧特曼', 3: '<Python從入門到放棄>'}
請輸入你想要的獎品,如果不想要,則輸入"n"退出!!!0
恭喜你獲得獎品: 布娃娃
請輸入你想要的獎品,如果不想要,則輸入"n"退出!!!1
恭喜你獲得獎品: 變形金剛