-1

I get an unexpected indent error in def calculte_interest_on_savings(savings)? The variable savings got defined above on the exact same line...

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)     


print_balance()

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    The problem, assuming your code in the question is indented the same as the real code you’re running, is that `def calculte…` is indented below the line above which reads `deduct(500)` - it should be at the same level of indent as that line. – DisappointedByUnaccountableMod Apr 26 '21 at 19:33
  • Does this answer your question? [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) – wjandrea Apr 26 '21 at 20:15
  • An unexpected indent is a syntax error, not a scope error. Although, it looks like scoping is also a problem in this code. – wjandrea Apr 26 '21 at 20:22
  • BTW, welcome to Stack Overflow! Please take the [tour] and read [ask]. – wjandrea Apr 26 '21 at 20:24

2 Answers2

0

I would write this way

def print_balance(balance):
    print("Your balance is " + str(balance))

def deduct(amount,balance):
    print("Your new balance is " + str(balance -  amount))
    savings = balance-amount
    return savings

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


balance = 1000
print_balance(balance)
savings=deduct(500,balance)
calculte_interest_on_savings(savings)
print_balance(balance)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
DevScheffer
  • 491
  • 4
  • 15
0

There is no reason to nest these functions.

def print_balance():
   balance = 1000
   print("Your balance is " + str(balance))
   savings = deduct(balance, 500)
   calculate_interest_on_savings(savings)  
  

def deduct(balance, amount):
    print("Your new balance is " + str(balance - amount))
    savings = balance - amount
    return savings
 

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


if __name__ == '__main__':
    print_balance()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Noah Gaeta
  • 181
  • 5