There are only a few minor things you need to consider.
1.You need to call your function, and since you want to print the returned value, you need to call inside the print. print(readable_timedelta(20))
2.There is no need of variable days = 20
inside the function. If you do so every time your function will give the same result, that is "2 week(s) and 6 day(s)".
3.Whenever program will reach return
statement anycode after that will never be executed, hence print()
will never be executed. Either use return
or print()
.
#Code with return
def readable_timedelta(days):
"""Print the number of weeks and days in a number of days."""
#to get the number of weeks we use integer division
weeks = days // 7
#to get the number of days that remain we use %, the modulus operator
remainder = days % 7
return "{ } week(s) and { } day(s)". format(weeks, remainder)
#Calling Function
print(readable_timedelta(20))
print(readable_timedelta(40))
If we use print inside a function definition, we can simply call the function with the required parameter.
#Code without return
def readable_timedelta(days):
"""Print the number of weeks and days in a number of days."""
#to get the number of weeks we use integer division
weeks = days // 7
#to get the number of days that remain we use %, the modulus operator
remainder = days % 7
print("{ } week(s) and { } day(s)". format(weeks, remainder))
#Calling Function
readable_timedelta(20)
readable_timedelta(40)