需求:用戶輸入一個價格,如果高於或低於真實價格都會給出提示並繼續猜價且最多償試次數5次,猜價成功則提示成功並退出遊戲。
realprice = 1000
#輸入價格
price = int(input("input your price:"))
#計次初始值
num = 1
#當輸入價格與真實價格不等並且次數未達到5進入循環
while (price != realprice and num < 5):
#累加計次
num += 1
#當價格過低時給出提示並要求繼續輸入價格
if price < realprice:
print("your price is low!")
price = int(input("please input your price:"))
#當價格過高時給出提示並要求繼續輸入價格
else:
print("your price is high!")
price = int(input("please input your price:"))
#退出循環後進一步判斷價格相等還是超出限制次數
if (price == realprice):
print("you got it!")
else:
print("You guess for 10 times!Game Over!")