【发布时间】:2016-12-03 02:02:15
【问题描述】:
我正在阅读Python Crash Course这本书,在其中一个练习中遇到了一点小问题。基本上,它要求您创建一个 while 循环,告诉用户输入他们的年龄,然后它会根据他们的年龄返回票的价格。这应该重复,直到用户键入“退出”。非常简单,除了我对如何将输入从整数(他们的年龄)转换为字符串(“退出”)感到困惑。每当我尝试键入 quit 时,我都会收到错误消息:“int() 以 10 为底的无效文字:'quit'”。这是我目前所拥有的:
age_prompt = "\nWrite in your age: "
age_prompt += "\nType 'quit' to exit "
while True:
age = int(input(age_prompt))
if age < 3:
print("Your ticket is free.")
elif age < 12:
print("Your ticket is $10")
else:
print("Your ticket is $15")
if age == 'quit':
break
【问题讨论】:
标签: python string loops while-loop integer