Python基础不太懂

从控制台分别输入年、月、日,通过计算打印出你输入的日期,是该年份的第多少天

望采纳


y,m,d=map(int,input('请输入年月日:').split(','))
n = [31,28,31,30,31,30,31,31,30,31,30,31]
n2 = [31,29,31,30,31,30,31,31,30,31,30,31]
a = 0
if y % 400==0 or (y % 4==0 and y%100!=0):
   for i in range(0,m-1):
    a=a + n2[i]
    z=a+d
   print('第{}天'.format(z))
else:
   for i in range(0,m-1):
    a= a+n[i]
    z2=a+d
   print('第{}天'.format(z2))

year=int(input('请输入年:'))
month=int(input('请输入月:'))
day=int(input('请输入天:'))
sum=day
days = [31,28,31,30,31,30,31,31,30,31,30,31]
i=0
if ( year%4 == 0 and year%100 != 0) or (year%400 == 0):
    days[1] = 29
while i< month-1:
    sum=sum+days[i]
    i+=1
print '这一天是该年的第',sum,'天'

供参考:
python常用日期函数
https://blog.csdn.net/weishuai90/article/details/128768200
常用例子
https://www.runoob.com/python3/python3-examples.html

from dateutil import parser

time_str = "2018 9 03"
dtime = parser.parse(time_str)
aa = dtime.timetuple()
print(aa)
print(aa.tm_yday)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
year, month, day = eval(input('请输入(年 月 日):'))

months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
sum = 0
for index in range(month):
    sum += months[index]
sum += day

print('你输入的日期是该年份的第%d天' % sum)

可以直接用python的datetime函数计算两个日期之间的差值

from datetime import date
year, month, day =map(int,input('请输入年月日,用英文逗号隔开:').split(','))
result = ( date(year, month, day) - date(year,1,1) ).days
print('你输入的日期是该年份的第%s天' % result)


year = int(input("Enter the year: "))
month = int(input("Enter the month: "))
day = int(input("Enter the day: "))

months = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
if 0 < month <= 12:
    sum = months[month - 1]
else:
    print("Invalid month")
sum += day
if month > 2 and year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
    sum += 1
print("It's the " + str(sum) + "th day of the year.")
该代码读入年、月、日,然后判断月份是否有效。如果有效,累加该月的天数和当天的天数。如果是闰年且月份大于 2,天数再加一。最后输出该天是该年的第几天。


该回答引用ChatGPT
请参考下面的解决方案,如果有帮助,还请点击 “采纳” 感谢支持!

代码如下:

from datetime import datetime

year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日:"))

input_date = datetime(year, month, day)
day_of_year = input_date.strftime("%j")

print("您输入的日期是:{}年{}月{}日".format(year, month, day))
print("这是{}年的第{}天".format(year, day_of_year))