2 回答

TA贡献1829条经验 获得超6个赞
我建议去掉active标志,只break在"quit"输入时 ing ,像这样,然后你可以安全地转换为int,因为如果"quit"输入代码将不会到达那个点:
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检查是不必要的,因为您已经使用较早的检查保证了它们。

TA贡献1786条经验 获得超13个赞
如果您想添加另一个提示,您可以在循环之前询问第一个提示,并在循环结束时询问另一个提示。如果你想添加价格,你需要一个变量。如果您不想提示其他问题但想要更多用户输入,请将提示留空。
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.
添加回答
举报