Return 作为参数给出的闰年列表的 True/False 布尔值列表
Return list of True/ False bool value for list of leap years given as an argument
我必须检查 year = [1900, 2020, 2021, 2001] 是否是闰年并得到 True/False 结果(布尔值)
我写了一个函数:
def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
但现在我需要将列表作为 year = [1900, 2020, 2021, 2001] 传递,当我这样做时,我收到错误消息“类型错误:% 不支持的操作数类型:'list' 和 'int'"
如何将列表作为参数传递给函数?
def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
year = [1900, 2020, 2021, 2001]
leap_year = list(map(is_leap_year,year))
print(leap_year)
你可以简单地这样做:
def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
years = [1900, 2020, 2021, 2001]
print(list(map(is_leap_year,years)))
我会做一个for循环:
for i in range(len(year)):
print(is_leap_year(year[i])
我必须检查 year = [1900, 2020, 2021, 2001] 是否是闰年并得到 True/False 结果(布尔值)
我写了一个函数:
def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
但现在我需要将列表作为 year = [1900, 2020, 2021, 2001] 传递,当我这样做时,我收到错误消息“类型错误:% 不支持的操作数类型:'list' 和 'int'"
如何将列表作为参数传递给函数?
def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
year = [1900, 2020, 2021, 2001]
leap_year = list(map(is_leap_year,year))
print(leap_year)
你可以简单地这样做:
def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
years = [1900, 2020, 2021, 2001]
print(list(map(is_leap_year,years)))
我会做一个for循环:
for i in range(len(year)):
print(is_leap_year(year[i])