python-tutorial/0006_1/if-elif-else.py

47 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

age = 20
if age < 18:
print("你是未成年人")
elif age >= 18 and age < 30:
print("你是年轻人")
else:
print("你是成年人")
"""
编写一个程序根据用户输入的分数0-100输出 “不及格”、“及格”、“良好”、“优秀”。
"""
score = float(input("请输入您的分数:"))
if score < 60:
print(f"{score}分不及格")
elif score == 60:
print(f"{score}分及格")
elif score < 85:
print(f"{score}分良好")
else:
print(f"{score}分优秀")
"""
应用:编写一个程序,根据用户输入的月份,输出对应的季节(如 “1” 对应 “冬季”)。
"""
month = int(input("请输入月份:"))
if month == 3 or month == 4 or month == 5:
print(f"{month}月为春季")
elif month == 6 or month == 7 or month == 8:
print(f"{month}月为夏季")
elif month == 9 or month == 10 or month == 11:
print(f"{month}月为秋季")
elif month == 12 or month == 1 or month == 2:
print(f"{month}月为冬季")
"""
挑战编写一个程序根据用户输入的年份判断是否为闰年能被4整除且不能被100整除或者能被400整除的年份为闰年
"""
year = int(input("请输入年份:"))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year}是闰年")
else:
print(f"{year}是平年")