三个出口:以另一种方式完成练习7-4 或练习7-5,在程序中采取如下所有做法。
- 在while 循环中使用条件测试来结束循环。
- 使用变量active 来控制循环结束的时机。
- 使用break 语句在用户输入’quit’时退出循环。
Jackey已对题目稍作修改。代码如下:
// An highlighted block
prompt = "How old are you? "
prompt += "\n(Enter 'quit' when you are finished.) "
active = True
while active:
age = input(prompt)
if age == 'quit':
print("\nYou have logged out of the system.Thank you.")
break
else:
age = int(age)
if age < 0:
print("\n\t▲▲▲ Good luck to you during pregnancy! ▲▲▲\n")
elif age <= 3:
print(f"\n\t▲▲▲ You have {age} old,the ticket for free. ▲▲▲\n")
elif age <= 12:
print(f"\n\t▲▲▲ You have {age} old,the ticket is 10$. ▲▲▲\n")
elif age <= 60:
print(f"\n\t▲▲▲ You have {age} old,the ticket is 15$. ▲▲▲\n")
elif age < 100:
print(f"\n\t▲▲▲ You have {age} old,the ticket for free. ▲▲▲\n")
else:
print("You're so funny. Bye!")
active = False