2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
我建議去掉active標(biāo)志,只break在"quit"輸入時(shí) ing ,像這樣,然后你可以安全地轉(zhuǎn)換為int,因?yàn)槿绻?quot;quit"輸入代碼將不會(huì)到達(dá)那個(gè)點(diǎn):
while True:
age = input(prompt)
if age == "quit":
break
age = int(age)
if age < 3:
print("Your ticket is $5")
elif age < 12:
print("Your ticket is $10")
else:
print("Your ticket is $15")
請注意,age >= 3和age >= 12檢查是不必要的,因?yàn)槟呀?jīng)使用較早的檢查保證了它們。

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
如果您想添加另一個(gè)提示,您可以在循環(huán)之前詢問第一個(gè)提示,并在循環(huán)結(jié)束時(shí)詢問另一個(gè)提示。如果你想添加價(jià)格,你需要一個(gè)變量。如果您不想提示其他問題但想要更多用戶輸入,請將提示留空。
prompt = "Enter your age for ticket price"
prompt += "\nEnter 'quit' to exit: "
price = 0
user_input = input(prompt)
while True:
if user_input == 'quit':
break
age = int(user_input)
if age < 3:
price += 5
elif age < 12:
price += 10
else:
price += 15
print(f"Your ticket is ${price}")
user_input = input("You can add an age to add another ticket, or enter 'quit' to exit.
添加回答
舉報(bào)