Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noob Udacity Help
#1
So I'm chugging along w/ a beginner's computer science course @ Udacity. It seems to be going ok, I don't have tons of time to dedicate to it, but I'm a few hours in, and I ran across this "homework" problem.

Now, I realize my code is going to be inefficient, I'm just going off the lessons already learned.

I'm on this problem, there is no answer/explanation after, it's just wrong or right. It's being flagged as wrong.

The thing is if I do a print statement with the function I defined and pass it the required arguments, I get the correct number, so it seems to me that the code is working.

But, there are these test statements that were already defined by Udacity at the bottom. Right now they are commented so that the code will run. But if I uncomment those test statements, it gives me errors. Specifically, it gives me "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'"

# By Websten from forums
#
# Given your birthday and the current date, calculate your age in days. 
# Account for leap days. 
#
# Assume that the birthday and current date are correct dates (and no 
# time travel). 
#

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
#    days = [email protected]_to_end_of_month(year1,month1,day1,year2,month2,day2) + remaining_days_in_birth_year(year1,month1,day1,year2,month2,day2) + days_in_full_years(year1,month1,day1,year2,month2,day2) + days_per_month_final_year(year1,month1,day1,year2,month2,day2) + days_in_final_month(year1,month1,day1,year2,month2,day2)
#    return days
    ##
    # Your code here.
    ##
    days = days_from_birth_to_end_of_month(year1,month1,day1,year2,month2,day2) + remaining_days_in_birth_year(year1,month1,day1,year2,month2,day2) + days_in_full_years(year1,month1,day1,year2,month2,day2) + days_per_month_final_year(year1,month1,day1,year2,month2,day2) + days_in_final_month(year1,month1,day1,year2,month2,day2)
    return days

days_in_ea_month = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
days_in_ea_month_leap = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

common_year_days = 365
leap_year_days = 366

def days_from_birth_to_end_of_month(year1, month1, day1, year2, month2, day2):
    if year1 == year2 and month1 == month2 and day1 == day2: #test to see if birth was today
        days = 0 # if today, 0 days old
        #print 'error code 1 ' + str(days)
        return days
    
    if year1 == year2 and month1 == month2: #test if birth was this month
        days = day2 - day1 #if only this month, only need to calculate difference between days
        #print 'error code 2 ' + str(days)
        return days
    
    if year1 != year2 and is_leap_year(year1) == True: 
        days = days_in_ea_month_leap[month1 - 1] - day1
        #print 'error code 3 ' + str(days)
        return days
    
    if year1 != year2 and is_leap_year(year1) == False:
        days = days_in_ea_month[month1 -1] -day1
        #print 'error code 4 ' + str(days)
        return days
    

def remaining_days_in_birth_year(year1,month1,day1,year2,month2,day2):
    if year1 == year2 and is_leap_year(year1) == True: #if year is the same, simply calculate from month1 to month2
        days = 0
        month = month1
        while days_in_ea_month_leap[month] < month2:
            days = days + days_in_ea_month_leap[month]
            month = month + 1
        #print 'error code 5 ' + str(days)
        return days
    
    if year1 != year2 and is_leap_year(year1) == True: #if year is different, calculate days starting next month to the end of the year
        days = 0
        month = month1
        while month < 12:
            days = days + days_in_ea_month_leap[month]
            month = month + 1
        #print 'error code 6 ' + str(days)
        return days
        
    if year1 != year2 and is_leap_year(year1) == False: 
        days = 0
        month = month1
        while month < 12:
            days = days + days_in_ea_month[month]
            month = month + 1
        #print 'error code 7 ' + str(days)
        return days

        
def days_in_full_years(year1,month1,day1,year2,month2,day2):
    if year2 - year1 < 2: # if there isnt a full year between then nothing to calculate
        days = 0
        #print 'error code 8 ' + str(days)
        return days
    
    if year2 - year1 >= 2: # make sure there is a full year between birth year and current year
        year = year1 + 1
        days = 0
        while year <= (year2 -1):
            if is_leap_year(year) == True:
                #print [email protected]_year(year)
                days = days + leap_year_days
                year = year + 1
            if is_leap_year(year) == False:
                #print is_leap_year(year)
                days = days + common_year_days
                year = year + 1
        #print 'error code 9 ' + str(days)
        return days
        
def days_per_month_final_year(year1,month1,day1,year2,month2,day2):
    if year1 == year2 or month2 == 1: #if year is same, or if Jan is final month, nothing to calculate here
        days = 0
        #print 'error code 10 ' + str(days)
        return days
        
    if year1 != year2 and month2 > 1 and is_leap_year(year2) == True:
        days = 0
        month = 0
        while month <= month2 -2:
            days = days + days_in_ea_month_leap[month]
            month = month + 1
        #print 'error code 11 ' + str(days)
        return days
        
    if year1 != year2 and month2 > 1 and is_leap_year(year2) == False:
        days = 0
        month = 0
        while month <= month2 -2:
            days = days + days_in_ea_month[month]
            month = month + 1
        #print 'error code 12 ' + str(days)
        return days
        
def days_in_final_month(year1,month1,day1,year2,month2,day2):
    days = day2
    #print 'error code 13 ' + str(days)
    return day2
        
    
        
    
def is_leap_year(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)


# Test routine

print daysBetweenDates(1979,4,2,2018,3,23)

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()
Reply
#2
This code so far Gclub
Reply
#3
Buehler?
Reply
#4
I haven't dug deeply but days_in_final_month has three ifs, and if none of them are satisfied, then it'll implicitly return None, and you'll get that problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Udacity while loop question liquidmetalrob 6 5,374 Jul-21-2017, 02:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020