Python 使用接受int()和str()的用户输入进行While循环
非常新的编程!我正在学习Python速成班这本书 尝试创建一个简单的程序来确定票价。将用户输入作为年龄,但在用户输入“完成”时结束程序 看起来像这样:Python 使用接受int()和str()的用户输入进行While循环,python,Python,非常新的编程!我正在学习Python速成班这本书 尝试创建一个简单的程序来确定票价。将用户输入作为年龄,但在用户输入“完成”时结束程序 看起来像这样: guest_age = ("Please enter your age. ") while True: age = input(guest_age) age = int(age) if age == 'done': break elif age < 3: print("You
guest_age = ("Please enter your age. ")
while True:
age = input(guest_age)
age = int(age)
if age == 'done':
break
elif age < 3:
print("Your ticket is free!")
elif age <= 12:
print("Your ticket is $10!")
else:
print("Your ticket is $15!")
在强制转换之前,您需要首先检查输入是否已完成。所以
guest_age = ("Please enter your age. ")
while True:
age = input(guest_age)
if age == 'done':
break
age = int(age)
...
在强制转换之前,您需要首先检查输入是否已完成。所以
guest_age = ("Please enter your age. ")
while True:
age = input(guest_age)
if age == 'done':
break
age = int(age)
...
没问题!如果这对您有效,请将其标记为解决方案,谢谢!没问题!如果这对您有效,请将其标记为解决方案,谢谢!