-2

I tried to run this code:

def readable_timedelta(days):
    days = 20
    """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)
    print (readable_timedelta)

and I keep getting this:

Your code displayed no output

Can anyone help with this?

Vega
  • 27,856
  • 27
  • 95
  • 103

2 Answers2

1

You need to actually call your function, for example

readable_timedelta(20)

Otherwise all you did was define a function, and as the note says there is no output from this program.

Side comment: there is no point in putting a print statement (or anything for that matter) after a return statement as no further code will execute and the function will return execution to the caller.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

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)
Khurshid
  • 458
  • 1
  • 5
  • 20