0

I know I'm missing something very simple, but I cannot for the life of me figure it out. Here's what I have right now.

def days_in_feb(user_year):
    if user_year % 100 == 0:
        if user_year % 400 == 0:
            user_year = print(f'{user_year} has 29 days in February.')
        else:
            user_year = print(f'{user_year} has 28 days in February.')
    else:
        if user_year % 4 == 0:
            user_year = print(f'{user_year} has 29 days in February.')
        else:
            user_year = print(f'{user_year} has 28 days in February.')
    return user_year
    
if __name__ == '__main__':
    user_year = int(input())
    print(f'{days_in_feb(user_year)}')

It will run fine once but then when it goes to take the next input, I get "days_in_feb() did not return a value. Your function may be missing a return statement." I think it has something to do with reassigning user_year to those print statements in the function but without them I don't know what to return.

Carson
  • 25
  • 6
  • 1
    print does not return a value. this will work fine if you drop all the print statements in your function and just return the strings – Michael Delgado Oct 13 '22 at 22:58
  • Your function's use of `user_year` doesn't make sense. Why return it at all? Why do `user_year = print()` when `print` only ever returns `None`? – Random Davis Oct 13 '22 at 22:58
  • 1
    next input? There's no loop here. Regardless. `print` function does not return anything – MYousefi Oct 13 '22 at 22:59
  • it sounds like you might be describing an error message from an autograder. dun dun dun. anyway please always include the [full traceback](//realpython.com/python-traceback) and the code which generated the error when asking about exceptions – Michael Delgado Oct 13 '22 at 23:01
  • You got me, it is an autograder. Shame on me. But yes that did help clear things up. I took out the "user_year=", swapped the print statements for return statements and it worked. Thank you for the help! – Carson Oct 13 '22 at 23:18

2 Answers2

0

Instead of having the function print out your statments about the selected year, have it return a value and then print this value.

It is also okay to use longer function names and explain what the function does.

You might also consider the single responsibility principle (SRP) and instead of returning a string, return an int. This way you'll have a higher reusability for the function.

And I think it is a good idea to tell the user what you are asking for, so i updated the input.

For example:

def get_days_in_feb_for_year(user_year):
    if user_year % 100 == 0:
        if user_year % 400 == 0:
            return 29
        else:
            return 28
    else:
        if user_year % 4 == 0:
            return 29
        else:
            return 28


if __name__ == '__main__':
    user_year = int(input('Please enter a year: '))
    days_in_feb = get_days_in_feb_for_year(user_year)
    print(f'{user_year} has {days_in_feb} days in February.')
Ovski
  • 575
  • 3
  • 14
-1

Maybe of topic, but return what you really want from the function instead of creating side effect of printing from the function.

def days_in_feb(user_year):
    if user_year % 100 == 0:
        if user_year % 400 == 0:
            return 29
        else:
            return 28
    else:
        if user_year % 4 == 0:
            return 29
        else:
            return 28
    
if __name__ == '__main__':
    user_year = int(input())
    print(f'{user_year} has {days_in_feb(user_year)} days in Feburary.')
OtherGuru
  • 1
  • 2