Python Function -> Scope exercise

Hey there,

I’m really stuck with this exercise: https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-python-functions/articles/python-scope-article

My solution but it is incorrect and I’m quite puzzled now with regards to nested functions and scope of variables:

def print_balance():

balance = 1000

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

def deduct(amount):

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

deduct(500)

def calculte_interest_on_savings(savings):

savings=balance-amount

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

def calculate_taxes():

savings = 500

tax_amount = savings * 0.13

print("You will be taxed: " + str(tax_amount))

calculate_taxes()

print_balance()

calculte_interest_on_savings()

Welcome to the forums :tada:!

You have defined this variable (balance) in a block scope. Instead, it should be in global scope which every function can get access to.

You have placed a variable called (savings) in the nested function. It should be placed in the first function called (def calculte_interest_on_savings()). So that the next print statement able to access (savings) variable which has a value of 500.

You should only invoke this code on the most bottom of the code which will return a value. FYI, replace deduct(500) with calculate_taxes().

Hope it helps :smile: