我很难对代码进行故障排除。我所要做的就是让用户进入开始和结束的一年,然后能够计算两者之间的所有闰年。我仍然是Python的新手,并且无法弄清楚为什么我的while循环没有做我想做的事情。
starting = int(raw_input('Enter starting year: '))
ending = int(raw_input('Enter ending year: '))
print 'Leap years between', starting, 'and', ending
while starting <= ending:
if starting % 4 == 0 and starting % 100 != 0:
print starting
if starting % 100 == 0 and starting % 400 == 0:
print starting
starting = starting + 1
这就是我现在所拥有的。我真的不希望没有给我答案,但如果我能得到一些暗示为什么我的while循环不起作用我会非常感激。
这是我在IDE中看到的输出:
答案 0 :(得分:2)
代码工作得很好。这是以下会议:
Enter starting year: 2008
Enter ending year: 2032
Leap years between 2008 and 2032
2008
2012
2016
2020
2024
2028
2032
这完全符合information give her e。
控制台会话:
>>> from leapyear import leap
>>> leap()
Enter starting year: >? 2008
Enter ending year: >? 2032
Leap years between 2008 and 2032
2008
2012
2016
2020
2024
2028
2032
def leap():
starting = int(raw_input('Enter starting year: '))
ending = int(raw_input('Enter ending year: '))
print 'Leap years between', starting, 'and', ending
while starting <= ending:
if starting % 4 == 0 and starting % 100 != 0:
print(starting)
if starting % 100 == 0 and starting % 400 == 0:
print(starting)
starting += 1
您也可以使用列表推导来执行此功能:
def new_leap(start, end):
return [x for x in xrange(start, end + 1)
if (x % 400 == 0) or (x % 4 == 0 and not x % 100 == 0)]
控制台会话:
>>> from leapyear import new_leap
>>> new_leap(2008, 2032)
[2008, 2012, 2016, 2020, 2024, 2028, 2032]
答案 1 :(得分:0)
我们在value = c中添加了所有无效案例 并刚匹配并从b
中删除了该值indexPath(for cell:)
答案 2 :(得分:0)
def is_leap(year):
leap = False
if year%4 ==0 :
leap = True
elif year%100 ==0:
leap = False
elif year%400 ==0:
leap = True
return leap
year = int(raw_input())
print is_leap(year)