python - 列出年份列表中闰年数量的功能
问题描述
我不是在寻找答案,而是在寻找正确方向的指针来自己解决这个问题。
我有一个问题,我有一个函数可以返回八年列表中的闰年数。我不确定如何处理这个问题。我尝试过使用 FOR LOOP,也尝试过同时使用 calendar.isleap 和 calendar.leapdays 方法,但我似乎遗漏了一些东西,因为我仍然遇到错误。
--- 代码如下 ---
def countLeapYears(yearList):
(学生代码在这里。)
print(countLeapYears([2001, 2018, 2020, 2090, 2233, 2176, 2200, 2982]))
print(countLeapYears([2001, 2018, 2020, 2092, 2204, 2176, 2200, 2982]))
我已经编辑并提出了这个问题,因为我的请求因看起来信息不足而被标记为暂停。所以上面是实际的问题。
我尝试的是这样的:
ly = 0
for i in yearList:
if i % 4 == 0:
ly += 1
谢谢。
解决方案
好的...感谢我得到的一些输入。在一些帮助下将我推向正确的方向,这就是我所拥有的。它有效,答案是我应该做的。
import calendar
def countLeapYears(yearList):
j=0
for i in listYears:
if calendar.isleap(int(i)):
j += 1
return j
预期输出:2
打印(countLeapYears([2001, 2018, 2020, 2090, 2233, 2176, 2200, 2982]))
预期输出:4
打印(countLeapYears([2001, 2018, 2020, 2092, 2204, 2176, 2200, 2982]))
推荐阅读
- java - 在后台每5分钟从android obb文件夹中删除文件
- npm - 在 Azure Devops 中使用缓存任务
- python - TypingError:在 nopython 模式管道中失败(步骤:nopython 前端) float32 类型的未知属性“形状”
- angular - 使用 Guard 读取功能模块的存储状态,以便在根级别中使用
- visual-studio-code - Visual Studio 代码中断
- python - ModuleNotFoundError:没有名为“redisgraph”的模块
- mongodb - 基于mongodb中子文档数组中的数组的条件计数?
- chart.js - Chart.Js饼图点击
- html - 神秘闪烁的黑线,几乎就像 NextJS 中的边框
- c++ - Qt -- QPlainTextEdit::extraSelections 覆盖 QSyntaxHighlighter