Python 我的代码没有给出输出,我需要一些数字

Python 我的代码没有给出输出,我需要一些数字,python,loops,python-3.x,while-loop,Python,Loops,Python 3.x,While Loop,我期望从上面的代码中输出一些数字,但我没有得到它。 我不熟悉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 d

我期望从上面的代码中输出一些数字,但我没有得到它。 我不熟悉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)
来自论坛的Websten的
#
#
#根据您的生日和当前日期,以天为单位计算您的年龄。
#补偿闰日。
#假设生日和当前日期是正确的日期(并且没有时间旅行)。
#简单地说,如果你出生于2012年1月1日,今天的日期是2012年1月2日
#你才1天大。
#
#暗示
#全年365天,闰年366天。
def下一天(年、月、日):
“”“简单版本:假设每个月有30天”“”
如果日期<30:
返回年、月、日+1
其他:
如果月份==12:
返回年份+1,1,1
其他:
返回年份、月份+1、1
def日期之间的日期(第1年、第1个月、第1天、第2年、第2个月、第2天):
“”“返回year1/month1/day1之间的天数
和year2/month2/day2。假设输入是有效日期
在格尔戈里历法中,第一个日期不是在
第二个
num=0
#你的代码在这里!
yearx=year1
monthx=month1
dayx=day1
而((第2年>=第1年)和(第2月>=第1月)和(第2天>=第1天)):
yearx,monthx,dayx=nextDay(yearx,monthx,dayx)
num=num+1
num='5'
返回数
打印日期之间的日期(2012,9,302012,10,30)

我从来没有掌握Python中的while语句,但我认为这是您的无限循环。day2>day1等总是正确的。因此,该条件仍然正确,因此您必须使用num递增

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

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

  • 如果年份相同
  • 如果年份相同,则计算它们之间的天数
  • 如果年份不同,则计算该特定年份的第一个日期与年末之间的天数
  • 计算第二个日期的年初到第二个日期之间的天数
  • 计算第一年年底和第二年年初之间的年数差,并将其转换为天数

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

    你需要换线:

    而((第2年>=第1年)和(第2月>=第1月)和(第2天>=第1天)):

    致:

    而((year2>=yearx)和(month2>=monthx)和(day2>=dayx)):

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


    此外,我认为当第2天的第x天大于第2天时,while循环将中断,因此您的测量值将减少1。

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

    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
    

    你的程序陷入了一个无限循环。你没有改变
    year2
    month2
    ,或
    day2
    year1
    month1
    ,或
    day
    ,所以你的while循环永远不会终止。
    year2
    year2非常感谢,我犯了错误,我不能投票,因为我有1分fyi,如果列表中的数字前面有一个空格,格式会更好。非常感谢我不知道,非常感谢,我犯了错误,我不能投票,因为我有1分没问题;)无限循环是鬼鬼祟祟的。如果我们更改年份,这仍然会返回错误答案,请尝试:
    (2012,9,302013,11,30)