Question regarding creating Classes & Objects

Viewed 43

I saw a code that doesn't work in my Visual Studio Code. Do I need to download another extension of Python or do normal Python work?

enter image description here

class Bank:
   def _init_(self):
      self.balance = 1000
   def get_balance(self):
      return self.balance
   def withdraw(self, amount):
      self.balance = self.balance - amount
      return amount
        
my_bank = Bank()
my_bank.withdraw(100)
balance = my_bank.get_balance()
print(balance)
my_bank.withdraw(50)
balance = my_bank.get_balance()
print(balance)
1 Answers

Your class is not initializing the balance variable because; _init_ in a class should be written with two underscores, not one.

class Bank:
   def __init__(self):
      self.balance = 1000
Related