I když to není tak elegantní, podle reference na faktoriál předpokládám, že domácí úkol je na procvičení rekurze.
Tedy třeba něco jako
def f(max_depth, prefix = '', cur_depth = 1):
  msg = prefix + str(cur_depth)
  print(msg)
  if cur_depth < max_depth:
      f(max_depth, msg, cur_depth + 1)
      print(msg)
f(3)