1. 输入三角形三个边,判断能否构成一个三角形
两边之和大于第三边
a = float(input('a '))
b = float(input('b '))
c = float(input('c '))
judge = True if a + b > c and a + c > b and b + c > a else False
if judge:
print('能构成三角形')
else:
print('不能构成三角形')
a 1
b 2
c 3
不能构成三角形
2. 输入一个年份,比如2020,判断是否是闰年。
def year():
y = int(input("请输入年份:"))
judge = (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0)
if judge:
print(y, '年是闰年')
else:
print(y, '年不是闰年')
year()
year()
请输入年份:2020
2020 年是闰年
请输入年份:2000
2000 年是闰年
请输入年份:2019
2019 年不是闰年
3. 获取当前系统时间,判断是上午,下午还是晚上。如果是上午,输出“Good morning!",否则输出”Good afternoon!“,如果是晚上输出”Good evening!“。
import datetime
hour = datetime.datetime.now().hour
# hour = int(input('输入'))
print(hour)
if 0 <= hour <= 12:
print('Good morning!')
elif 12 < hour <= 18:
print('Good afternoon!')
else:
print('Good evening!')
13
Good afternoon!
第三个没有写的太精细,上下午分界不明显。
:- )