鱼C论坛

 找回密码
 立即注册
查看: 1487|回复: 4

新人每日一问,求某个日期是该年的第几天

[复制链接]
发表于 2022-3-19 21:32:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 fengjianyx 于 2022-3-20 19:23 编辑

新人初学,尝试练习基础代码:

for语句理解不透彻,导致方案一的错误,
求各位大佬讲解一下,具体应该怎么去考虑for循环后的输出值对齐?
代码如下:方案一结果错的,方案二调整print缩进就好了



【题目】计算某个日期是这一年的第几天,


【方案一】运行到最后结果是多行的
  1. #判断日期是第几天
  2. str1 = str(input("请输入一个日期,格式为2020/12/22\n"))
  3. year =  int(str1[0:4])
  4. month = int(str1[5:7])
  5. day   = int(str1[8:10])
  6. #定义月份的天数
  7. list=[31,28,31,30,31,30,31,31,30,31,30,31]
  8. #先判断是否平年闰年,用于判断2月份天数
  9. if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
  10.     list[1] = 29
  11. else:
  12.     list[1] = 28
  13. #for语句控制月份的循环
  14. for i in range(1,month):
  15.     if month == 1:
  16.         print(day)
  17.         break
  18.     day+=list[i] #
  19.     print(day)
复制代码



结果为:
请输入一个日期,格式为2020/12/22
2022.12.31
59
90
120
151
181
212
243
273
304
334
365





【方案二】经过调整print对齐至for后又正常如下:

  1. #判断日期是第几天
  2. str1 = str(input("请输入一个日期,格式为2020/12/22\n"))
  3. year =  int(str1[0:4])
  4. month = int(str1[5:7])
  5. day   = int(str1[8:10])
  6. #定义月份的天数
  7. list=[31,28,31,30,31,30,31,31,30,31,30,31]
  8. #先判断是否平年闰年,用于判断2月份天数
  9. if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
  10.     list[1] = 29
  11. else:
  12.     list[1] = 28
  13. #for语句控制月份的循环
  14. for i in range(1,month):
  15.     if month == 1:
  16.         print(day)
  17.         break
  18.     day+=list[i] #
  19. print (str1,"是{}年的第{}天".format(year,day))
复制代码



结果准确了:
请输入一个日期,格式为2020/12/22
2022.12.31 是2022年的第365天
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-19 22:41:52 | 显示全部楼层
1、列表的下标从0开始,你range(1,month)就直接把一月给跳了
2、range应该是 month-1, 如果month = 2 时,按照你原来的那就是 0 1 =》 1月和二月,多了一个月
输出来的结果就不对了

2022/12/31
2022/12/31 是2022年的第365天

2022/02/01
2022/02/01 是2022年的第32天

  1. # 判断日期是第几天
  2. str1 = str(input("请输入一个日期,格式为2020/12/22\n"))
  3. year = int(str1[0:4])
  4. month = int(str1[5:7])
  5. day = int(str1[8:10])
  6. # 定义月份的天数
  7. list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  8. # 先判断是否平年闰年,用于判断2月份天数
  9. if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
  10.     list[1] = 29
  11. else:
  12.     list[1] = 28
  13. # for语句控制月份的循环
  14. for i in range(0, month-1):  #
  15.     if month == 1:
  16.         print(day)
  17.         break
  18.     day += list[i]
  19. print(str1, "是{}年的第{}天".format(year, day))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2022-3-20 09:07:27 | 显示全部楼层
后面for循环的range的mouth应该是mouth-1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-21 17:20:00 | 显示全部楼层
你把输出放入循环内 当然就会输出很多结果 你只需要最后的结果 输出放到循环外就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-21 17:44:49 | 显示全部楼层
  1. #判断日期是第几天
  2. str1 = str(input("请输入一个日期,格式为xxxx/xx/xx\n"))
  3. year =  int(str1[0:4])
  4. month = int(str1[5:7])
  5. day = int(str1[8:10])
  6. while True:
  7.     if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12) and day > 31:
  8.         str1 = str(input("请输入一个正确的日期,格式为xxxx/xx/xx\n"))
  9.         year =  int(str1[0:4])
  10.         month = int(str1[5:7])
  11.         day = int(str1[8:10])
  12.     elif (month == 4 or month == 6 or month == 9 or month == 11) and day > 30:
  13.         str1 = str(input("请输入一个正确的日期,格式为xxxx/xx/xx\n"))
  14.         year =  int(str1[0:4])
  15.         month = int(str1[5:7])
  16.         day = int(str1[8:10])
  17.     elif month == 2:
  18.         if ((year % 4 == 0 and year % 100 != 0) or year % 400 == 0) and day > 29:
  19.             str1 = str(input("请输入一个正确的日期,格式为xxxx/xx/xx\n"))
  20.             year =  int(str1[0:4])
  21.             month = int(str1[5:7])
  22.             day = int(str1[8:10])
  23.         elif year % 4 != 0 and day > 28:
  24.             str1 = str(input("请输入一个正确的日期,格式为xxxx/xx/xx\n"))
  25.             year =  int(str1[0:4])
  26.             month = int(str1[5:7])
  27.             day = int(str1[8:10])
  28.         else:
  29.             break
  30.     else:
  31.         break
  32. #定义月份的天数
  33. list1=[31,28,31,30,31,30,31,31,30,31,30,31]
  34. #先判断是否平年闰年,用于判断2月份天数
  35. if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
  36.     list1[1] = 29
  37. #for语句控制月份的循环
  38. for i in range(0, month-1):
  39.     day+=list1[i]
  40. print (str1,"是{}年的第{}天".format(year,day))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 13:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表