Beginner- Python Object Oriented Programming

Hi, I’m new to coding and learning object oriented programming. I am having trouble with implementing a class-- what am I doing wrong?

I have the following requirements: must have a name and address, Functions required are:

  1. create_account(first_name, last_name, initial_deposit)
    → Return account_id (a String)
  2. deposit(account_id, amount)
  3. withdraw(account_id, amount)
    → transfer(from_account_id, to_account_id, amount)
  4. accounts()
    –>Returns a list/array of account_ids
  5. account(account_id)
    → Returns and prints the specific account

My python code is as follows:

class WesterosBankAccount: def __init__(self, firstname, lastname, balance, address): self.firstname = firstname self.lastname = lastname self.balance = balance self.address = address print("Welcome to The Iron Bank of Braavos") self.owner = input("\n---> Enter primary bank account holder name: ") def deposit(self, amount): amount = float(input("\nAmount of Dragon Coin to be deposited is: ")) self.balance += amount print("\n--->Amount Deposited %.2f:" % amount, "Dragon Coins") print() def withdraw(self, amount): amount = float(input("Amount of Dragon Coin to be withdrawn is: ")) if self.balance >= amount: self.balance -= amount print("\n--->Amount Withdrawn %.2f:" % amount, "Dragon Coins") else: print("\n>>EAT CROW! No Dragon Coins for you fool<<") def dump(self): s = '%s, %s, balance: %s' % \ (self.firstname, self.lastname, self.balance, self.address) print ("s") #print(account1) account1.firstname = 'Tyrion' account1.lastname = 'Lannister' account1.balance = 10000 account1.address = "2222 New York St, NY, NY 12345" # create object of class s = WesterosBankAccount() # Calling functions with class object s.deposit() s.display() s.withdraw() s.dump()

You have a constructor/initializer to assign the .firstname , .lastname , .balance , and .address
so

account1.firstname = 'Tyrion'
account1.lastname = 'Lannister'
account1.balance = 10000
account1.address = "2222 New York St, NY, NY 12345"

is not necessary.
That can be replaced by
account1 = WesterosBankAccount('Tyrion', 'Lannister', 10000, "2222 New York St, NY, NY 12345")
or
s = WesterosBankAccount('Tyrion', 'Lannister', 10000, "2222 New York St, NY, NY 12345")
depending on whether you want that object to be s or account1 ,
because the __init__ method (function) has the parameters in that order.

And then you’d use the same object to do the .deposit , .display() , .withdraw , and .dump()

Note that the deposit and withdraw methods have a parameter other than self
so .deposit() and .withdraw() require an argument.
like
s.withdraw(200)
and
s.deposit(100)

1 Like