Add if-elif-else
This commit is contained in:
parent
ed8b74a8c6
commit
75ba7fdf53
47
0007/if-elif-else.py
Normal file
47
0007/if-elif-else.py
Normal file
@ -0,0 +1,47 @@
|
||||
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}是平年")
|
||||
Loading…
x
Reference in New Issue
Block a user