if-elif-else的条件测试使用:
1 """ 2 人生的不同阶段 3 """ 4 age = int(input("Enter your age:")) 5 while age != 0: 6 if age < 2: 7 print("You are baby!") 8 elif age >= 2 and age < 4: 9 print("You are toddler!") 10 elif age < 13: 11 print("You are child!") 12 elif age < 20: 13 print("You are teenager!") 14 elif age < 65: 15 print("You are adult!") 16 else: 17 print("You are old people!") 18 age = int(input("Enter your age:"))
其中
age >= 2 and age < 4 多余,直接为age < 4即可在这个结构中实现2(含)~4
真的是实践出真知