该回答引用chatgpt:
year = int(input("请输入一个正整数年份:"))
# 判断是否为闰年
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
days = 366 # 闰年有366天
else:
days = 365 # 平年有365天
print(f"{year}年有{days}天。")
"""
题目:根据用户输入的半径计算圆的周长和面积
作者:假书生@
日期:2020年1月6日
"""
import math #导入math数学函数库
r = input("请输入您计算机的半径(保留两位小数):") # 使用input()函数获取到用户输入的参数
r = float(r) #将用户输入的参数转换成浮点数
pi = math.pi #使用math.pi定义圆周率
c = 2 * pi * r #使用周长公式计算圆的周长
s = r ** 2 * pi #使用面积公式计算圆的面积
print ("该圆的周长为{:.2f},该圆的面积为{:.2f}".format(c,s)) #使用Print方法输出圆的周长和面积
我可以解决该问题。以下是解决方案:
import datetime
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
year = int(input("请输入年份:"))
if is_leap_year(year):
days_of_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
days_of_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month = int(input("请输入月份:"))
day = int(input("请输入日:"))
day_count = sum(days_of_month[:month-1]) + day
if is_leap_year(year) and month > 2:
day_count += 1
print(f"{year}年的第{day_count}天")
代码解释:
对于2021年1月1日,执行以上代码可以获得输出:2021年的第1天。