Lakshmi Chitti Lakshmi Chitti - 1 year ago 182
Python Question

My code is not giving output , I expected some number

I am expecting some number as output from the above code,but I am not getting it out.
I am new to python but started coding with PHP.
Sorry if I go wrong some where.thanks

# 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)

Answer Source

You need to change the line:

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

to:

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

because you are not changing the value of month1 in your code but that of monthx.

Also, I think your while loop will break when dayx is greather that day2, so your measurement will be off by 1.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download