【发布时间】:2013-06-12 17:32:19
【问题描述】:
我期望从上面的代码中输出一些数字,但我没有把它弄出来。 我是 python 新手,但开始使用 PHP 编码。 对不起,如果我在哪里出错了。谢谢
# By Websten from forums
#
# Given your birthday and the current date, calculate your age in days.
# Compensate for leap days.
# Assume that the birthday and current date are correct dates (and no time travel).
# Simply put, if you were born 1 Jan 2012 and todays date is 2 Jan 2012
# you are 1 day old.
#
# Hint
# A whole year is 365 days, 366 if a leap year.
def nextDay(year, month, day):
"""Simple version: assume every month has 30 days"""
if day < 30:
return year, month, day + 1
else:
if month == 12:
return year + 1, 1, 1
else:
return year, month + 1, 1
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
"""Returns the number of days between year1/month1/day1
and year2/month2/day2. Assumes inputs are valid dates
in Gergorian calendar, and the first date is not after
the second."""
num = 0
# YOUR CODE HERE!
yearx = year1
monthx = month1
dayx = day1
while ((year2 >= year1 ) and ( month2 >= month1 ) and ( day2 >= day1 ) ) :
yearx,monthx,dayx = nextDay(yearx,monthx,dayx)
num = num + 1
num = '5'
return num
print daysBetweenDates(2012,9,30,2012,10,30)
【问题讨论】:
-
你的程序陷入了无限循环。
-
您不会更改
year2、month2或day2或year1、month1或day,因此您的 while 循环永远不会终止。 -
year2 <= 2012 and month1 <= 9 and day1 <= 30然后它在某个时候 month1 = 10 它应该停止。对吗?
标签: python loops python-3.x while-loop