Print element from a specific position in the list

1

I need to make it print the number of days in August in the list, but without using the:

def how_many_days(month_number):

    """Returns the number of days in a month.
    WARNING: This function doesn't account for leap years!
    """

days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]

#todo: return the correct value

# This test case should print 31, the number of days in the eighth month, August

print(how_many_days(8))
    
asked by anonymous 22.06.2017 / 16:55

3 answers

1

Your function is not returning any results, you need to call the return at the end of the function by passing the month number -1 , -1 is array starts counting at 0 instead of 1

def how_many_days(month_number):
  """Returns the number of days in a month.
  WARNING: This function doesn't account for leap years!
  """
  days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
  return days_in_month[month_number - 1]

#todo: return the correct value
# This test case should print 31, the number of days in the eighth month, August
print(how_many_days(8))
    
22.06.2017 / 17:01
1

There are two errors in your code:

  • Missing return in your function how_many_days
  • Python needs correct indentation.
  • For your code to work you need to put return in the function and arrange the indentation and as python starts in 0 you need to return the number of the month - 1, see:

    def how_many_days(month_number):
        days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
        return days_in_month[month_number-1];
    # This test case should print 31, the number of days in the eighth month, August
    
    print(how_many_days(8))
    

    See working at Ideone .

        
    22.06.2017 / 17:08
    -1
     def how_many_days(month_number):
    
       """Returns the number of days in a month.
          WARNING: This function doesn't account for leap years!
       """
    
       days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
    
       return days_in_month[month_number]          
    print(how_many_days(7));
    

    That's right.

        
    22.06.2017 / 17:28