0

So I made a code in oop where it would allow you to deposit money in a bank, and also withdraw it. I solved the part where I was able to deposit money but I'm not able to withdraw money from the bank. I would appreciate a fixed answer. Thanks!

class Bank:
    def __init__(self,name,accountNumber,totalBalance):
        self.name = name
        self.accountNumber = {}
        self.totalBalance = {}
    def deposit(self):
        print("What is your account number?")
        x = input()
        print("What balance are you depositing?")
        y = input()
        self.accountNumber[x] = y
        print(self.accountNumber)
    def withdraw(self):
        print("What is your account number?")
        s = int(input())
        print("What balance are you withdrawing?")
        f = int(input())
        self.accountNumber[s] = self.accountNumber[s] - f
        print(self.accountNumber)
Bank1 = Bank("MONEYYYYY",1234,53)
Bank1.deposit()
Bank1.withdraw()

If you know why it's not working then I would appreciate it if you could tell me why and fix it, thanks!

Ravishankar D
  • 131
  • 10

1 Answers1

0

There are other problems with the design of your solution, but to address this particular one, you are subtracting the withdrawal amount from self.accountNumber[s] instead of from self.totalBalance. You are also setting the deposit amount in the same incorrect field.

class Bank:
    def __init__(self,name,accountNumber,totalBalance):
        self.name = name
        self.accountNumber = {}
        self.totalBalance = {}
    def deposit(self):
        print("What is your account number?")
        x = input()
        print("What balance are you depositing?")
        y = input()
        if self.totalBalance.has_key(x) == False:
            self.totalBalance[x] = 0
        self.totalBalance[x] += y 
        print(self.accountNumber)
    def withdraw(self):
        print("What is your account number?")
        s = int(input())
        print("What balance are you withdrawing?")
        f = int(input())
        if self.totalBalance.has_key(s) == False:
            self.totalBalance[s] = 0
        self.totalBalance[s] = self.totalBalance[s] - f
        print(self.accountNumber)
Bank1 = Bank("MONEYYYYY",1234,53)
Bank1.deposit()
Bank1.withdraw()
RWRkeSBZ
  • 723
  • 4
  • 11