FAQ: Recursion: Python - Building Our Own Call Stack

This community-built FAQ covers the “Building Our Own Call Stack” exercise from the lesson “Recursion: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Recursion: Python

FAQs on the exercise Building Our Own Call Stack

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is this not working, is there a bug somewhere?

def sum_to_one(n):
  result = 1
  call_stack = []
  return result, call_stack
  while n!=1:
    execution_context = {"n_value" : n}
    call_stack.append(execution_context)
    n -= 1
    print(call_stack)

Even though it looks ok it wont let me do 3rd exercise, I’g getting error asking " Did you .append() the execution_context variable to call_stack ?"

How are you measuring that it looks ok?
Have you for example tried it and observed that it has the desired effect?

Ok, now I see I missed the return statement being in there in a middle. I know how I made this mistake (so at least I know what I need to work on), after completing the first part (where we create a return statement with two returns) I just continued with second part without moving return to the bottom of a function.

Codecademy or how to make recursion more complicated than it already is…

1 Like

It does go out of its way to be impenetrable. Since I already know a little about it I just avoided the explanation text as it seemed cryptic.

Why is return result, call_stack necessary here? What is the return function doing when the print function is already printing what we need.

def sum_to_one(n):
  result = 1
  call_stack = []
  
  while n != 1:
    execution_context = {"n_value": n}
    call_stack.append(execution_context)
    n -= 1
    print(call_stack)
  print("BASE CASE REACHED")
  return result, call_stack

sum_to_one(4) 

Can someone explain what the dictionary holds? What or who is “n_value”?