【问题标题】:My code is not giving output , I expected some number我的代码没有输出,我希望有一些数字
【发布时间】: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)

【问题讨论】:

  • 你的程序陷入了无限循环。
  • 您不会更改 year2month2day2year1month1day,因此您的 while 循环永远不会终止。
  • year2 &lt;= 2012 and month1 &lt;= 9 and day1 &lt;= 30 然后它在某个时候 month1 = 10 它应该停止。对吗?

标签: python loops python-3.x while-loop


【解决方案1】:

你需要换行:

while ((year2 >= year1) and (month2 >= month1) and (day2 >= day1)):

到:

while ((year2 >= yearx) and (month2 >= monthx) and (day2 >= dayx)):

因为您在代码中更改的不是month1的值,而是monthx的值。

另外,我认为当 dayx 大于 day2 时,您的 while 循环会中断,因此您的测量值将偏离 1。

【讨论】:

  • 非常感谢,我弄错了,我不能投票,因为我有 1 分
  • 没问题 ;) 无限循环是偷偷摸摸的。
  • 如果我们更改年份,这仍然会返回错误答案,请尝试:(2012,9,30,2013,11,30)
【解决方案2】:

我从来没有掌握 Python 中的 while 语句,但我认为这是你的无限循环,day2 > day1 等总是正确的。所以这个条件仍然正确,因此你被困在 num 增加中

会发生什么 - 您是否收到任何错误消息?

如果我这样做,我会设置函数来确定

  1. 如果年份相同
  2. 如果年份相同,则计算它们之间的天数
  3. 如果年份不同,则计算该特定年份的第一个日期和年底之间的天数
  4. 计算第二个日期的年初到第二个日期之间的天数
  5. 计算第一年年底和第二年年初之间的年数差异,并将其转换为天数

它可能很笨重,但它应该能让你回家

【讨论】:

  • year2 &lt;= 2012 and month1 &lt;= 9 and day1 &lt;= 30 然后它会在某个时候 month1 = 10 它应该停止。对吧?,我没有收到任何错误。&gt;&gt;&gt; ================================ RESTART ================================ &gt;&gt;&gt; 我明白了
  • 非常感谢,我弄错了,我不能投票,因为我有 1 分
  • 仅供参考,如果列表中的数字前面有一个空格,则格式会更好。
  • 非常感谢我不知道
【解决方案3】:

这是我在一个函数中的解决方案

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
##
# Your code here.
##
a1=str(month1)+'/'+str(day1)+'/'+str(year1)
date1 = datetime.strptime(a1, '%m/%d/%Y')

a2=str(month2)+'/'+str(day2)+'/'+str(year2)
date2 = datetime.strptime(a2, '%m/%d/%Y')

result= (date2 - date1).days
return result

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2015-05-30
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多