Objects and Attributes

Define a class to represent a bank account.Include the details like Name of the depositor,# Account number,type of account, balance amount in the account, Write methods, to assign initial values;# to deposit an amount;withdraw an amount after checking the balance;to display name,account number,# account type,and balance

class Customer:

    def getData(self, name, accno, acctype, balance):
        self.name = name
        self.accno = accno
        self.acctype = acctype
        self.balance = balance

    def displayCustomer(self):
        print("Customer name: ", self.name)
        print("Account No: ", self.accno)
        print("Account Type :", self.acctype)
        print("Balance: ", self.balance)

    def deposit(self, depo_amount):
        self.balance = self.balance + depo_amount

    def withdrawal(self, wi_amount):
        if self.balance - wi_amount < 0:
            print("Insufficient balance")
        else:
            self.balance = self.balance - wi_amount


    ch = 0
    while ch!=5:
        print("1. New Customer")
        print("2.Deposit")
        print("3.Withdrawal")
        print("4.Display")
        print("5.Exit")
        ch=int(input("Enter your choice"))
        obj=Customer()
        if ch==1:
            n=input("Enter name")
            no=int(input("Enter the account no:"))
            ty=input("Enter the account type")
            b=int(input("Enter the amount"))
            obj.getData(n,no,ty,b)
        if ch==2:
            b=int(input("Enter the amount to be deposited"))
            obj.deposit(b)
        if ch==3:
            b=int(input("Enter the amount to be withdrawn"))
            obj.withdrawal(b)
        if ch==4:
            obj.displayCustomer()
        if ch==5: break
        print("Program terminated")

Some thing wrong with this code. can anyone help ? Following is the error message:
“C:\Users\Administrator\PycharmProjects\Bank Account\venv\Scripts\python.exe” “C:/Users/Administrator/PycharmProjects/Bank Account/main.py”

  1. New Customer
    2.Deposit
    3.Withdrawal
    4.Display
    5.Exit
    Enter your choice1
    Traceback (most recent call last):
    File “C:/Users/Administrator/PycharmProjects/Bank Account/main.py”, line 6, in
    class Customer:
    File “C:/Users/Administrator/PycharmProjects/Bank Account/main.py”, line 38, in Customer
    obj=Customer()
    NameError: name ‘Customer’ is not defined

Process finished with exit code 1

I corrected the indentation. But still it is not working. The new code is this:

Define a class to represent a bank account.Include the details like Name of the depositor,

Account number,type of account, balance amount in the account, Write methods, to assign initial values;

to deposit an amount;withdraw an amount after checking the balance;to display name,account number,

account type,and balance

class Customer:

    def getData(self, name, accno, acctype, balance):
        self.name = name
        self.accno = accno
        self.acctype = acctype
        self.balance = balance

    def displayCustomer(self):
        print("Customer name: ", self.name)
        print("Account No: ", self.accno)
        print("Account Type :", self.acctype)
        print("Balance: ", self.balance)

    def deposit(self, depo_amount):
        self.balance = self.balance + depo_amount

    def withdrawal(self, wi_amount):
        if self.balance - wi_amount < 0:
            print("Insufficient balance")
        else:
            self.balance = self.balance - wi_amount

ch = 0
while ch!=5:
    print("1. New Customer")
    print("2.Deposit")
    print("3.Withdrawal")
    print("4.Display")
    print("5.Exit")
    ch=int(input("Enter your choice"))
    obj=Customer()
    if ch==1:
        n=input("Enter name")
        no=int(input("Enter the account no:"))
        ty=input("Enter the account type")
        b=int(input("Enter the amount"))
        obj.getData(n,no,ty,b)
    if ch==2:
        b=int(input("Enter the amount to be deposited"))
        obj.deposit(b)
    if ch==3:
        b=int(input("Enter the amount to be withdrawn"))
        obj.withdrawal(b)
    if ch==4:
        obj.displayCustomer()
    if ch==5: break
        print("Program terminated")

I removed that line. Still it is not working. I fear I have omitted some important lines in the code. This is the new error message.

Enter your choice4
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/Bank Account/main.py", line 51, in <module>
    obj.displayCustomer()
  File "C:/Users/Administrator/PycharmProjects/Bank Account/main.py", line 15, in displayCustomer
    print("Customer name: ", self.name)
AttributeError: 'Customer' object has no attribute 'name'

Process finished with exit code 1

Yes. I am going to re-write the program.

In the light of your comments, I scrutinized my code. There was an indentation error and wrong placement of one code line. I corrected it. Now it is working.
Thank you.

Of course. This code is for single customer. For multiple customers the entire structure of the code has to be changed

OK. Understood. I am trying to change the code.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.