0

I'm studying python right now, and I was set a task where I had to create a recursive function that returns the timetable of a certain number (x) up to (n), which would return the output to be printed where it is called, and

When I print the code from inside the function as below then it works normally:

def timeTable(n, x, tablesList):

    if n == 1:
        tablesList.insert(0, (str(n) + " x " + str(x) + " = " + str(n*x)))
        print ("\n".join(results))
        
    else:
        tablesList.insert(0, (str(n) + " x " + str(x) + " = " + str(n*x)))
        timeTable(n-1, x, tablesList)
       

(timeTable(12, 5, []))

This gives an expected output of:

1 x 5 = 5

2 x 5 = 10

3 x 5 = 15

4 x 5 = 20

5 x 5 = 25

6 x 5 = 30

7 x 5 = 35

8 x 5 = 40

9 x 5 = 45

10 x 5 = 50

11 x 5 = 55

12 x 5 = 60

However when I try and print the output when I call the function as below it returns "none" rather than the timetable

def timeTable(n, x, tablesList):

    if n == 1:
        tablesList.insert(0, (str(n) + " x " + str(x) + " = " + str(n*x)))
        return ("\n".join(results))
        
    else:
        tablesList.insert(0, (str(n) + " x " + str(x) + " = " + str(n*x)))
        timeTable(n-1, x, tablesList)
       

print(timeTable(12, 5, []))

Any help on why this is happening would be greatly appreciated.

Sid
  • 1

1 Answers1

0

When n == 1, after inserting in the tablesList array, you have to return tableList. And from the caller function or statement, you have to receive that array & print it.

Faruk Ahmad
  • 128
  • 10