Python Function -> Scope exercise

Dear all,

I’m still stuck with this Scope exercise. Why is there an unexpected indent error in def calculate interest on savings?
I defined savings above on the exact line as “def calculate interest on savings”.

Besides this problem, any resources to work on scoping issues with regards to nested functions?

Problem:


def print_balance():

  balance = 1000

  print("Your balance is " + str(balance))

  def deduct(amount):

    print("Your new balance is " + str(balance -  amount))

    savings = balance-amount

  deduct(500)

    def calculte_interest_on_savings(savings):

      print("You will gain interest on: " + str     (savings))

    calculte_interest_on_savings(savings)

Exercise URL: https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-python-functions/articles/python-scope-article


The goal of this exercise is to have you correct an error caused by indentation and nested functions. Yes, this indent error is unexpected, the exercise asks you to fix it. Can you figure out how?


In regards to the rest of the program, be careful with the indentation. Try calling deduct(100) outside of any block or function. You’ll get an error saying that deduct() doesn’t exist. Can you fix your errors?


This specific exercise in the article is asking you to correct an error caused by the use of nested functions, not to create nested functions.

1 Like

Hey there,

Thank you for your reply. I still don’t figure out how to fix the error. Could you just send me the solution so I do not waste too much time on the scope exercise as a beginner.

Thanks a lot!

The policy on the forums is to not just send the solution.

Scope is a very important aspect of programming. Skip over it now, and you risk not understanding many things.


Think about how the deduct function ends here

After that, everything else should be in line with the def deduct line. Why have you indented def calculate_interest…?

2 Likes

I indented def calculate_interest so that it finds the variable savings from the deduct function?

Well, all that happens without real help is that I get discouraged as I still dont get it…

We want you to learn to debug and fix these problems yourself, not rely on someone else’s solution.

To reiterate, this exercise is asking you to fix the issue caused by nested functions. Not to create nested functions. Are you sure you understand scope? Without a solid understanding of it, this exercise is going to be impossible to tackle.


This doesn’t work the way you’ve indented your code. Reset the exercise, then continue reading. We want the program to output the following:

Your balance is 1000
Your new balance is 500
You will gain interest on: 500
You will be taxed: 65.0

Run the code without making any changes. You get a Traceback error saying balance is not defined. How can you ensure that deduct() has access to the balance variable while still being callable at the global level (meaning deduct(500) on line 18 will still work)?


I also suggest you reread the article on scope.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.