我一直在研究这个程序,任何形式的帮助都将不胜感激。
查找两年之间的闰年并将其添加到数组中的程序...
from array import array
x=int(input("Enter the year "))
print("the year you entered is",x)
while x<=2017:
if x%4==0:
print(x)
n=array('i',[x])
n.append(x)
x=x+1
else:
x=x+1
print(n)
输出
enter the year 1992
the year you entered is 1992
1992
1996
2000
2004
2008
2012
2016
array('i', [2016, 2016])
请您参考如下方法:
问题是每当一年被 4 整除时,您都会重新设置数组的值。您要做的是在循环外声明数组。
from array import array
x=int(input("enter the year from which you want to know the leap year from"))
print("the year you entered is",x)
n=array('i')
while x<=2017:
if (x % 4 == 0 and x % 100 != 0) or x % 400 == 0:
print(x)
n.append(x)
x += 1 # we need to add 1 regardless, no need for else
print(n)
# output: array('i', [1992, 1996, 2000, 2004, 2008, 2012, 2016])
