0
私は現在、うるう年を含む2つの日付の間の何日かを数えるというこの問題に取り組んでいます。
しかし、2ヶ月と曜日が同じではないにもかかわらず、最初からループをスキップし続けますか?3条件python whileループで2つの日付が同じかどうかのチェックについて
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
day_count = 0
while (year2 != year1) and (month2 != month1) and (day2! = day 1):
# print "in loop" // tester
if (day2 != 0):
day2 = day2 - 1
day_count = day_count + 1
else:
if(month2 != 0):
month2 = month2 - 1
if month2 == (9 or 4 or 6 or 11):
day2 = 30
if month2 == 2:
day2 = 28
if (month2 == 2) and (year2 % 4):
day2 = 29
else:
day2 == 31
else:
year2 = year2 - 1
month2 = 12
#print day_count //tester
return day_count
# Test routine
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2012,1,1,2012,3,1), 60),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585),
((1900,1,1,1999,12,31), 36523)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()
私はあまりにも多くのプログラマーがこの多くのコードをデバッグしようとしています。間違いを見つけるために、いくつかの診断文を入力する必要があります。それは一つのことです。もう一つは、あなたがホイールを再発明しているということです。日付を扱うためのPythonライブラリがいくつかあり、その使用法に関する多くの質問があります。クイックアンサーのためだけにそれらを照会する必要があります。 –