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

Are you sure you have your code indented correctly? Where should the ch = 0 and while loop be located?

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’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

The error I see is related to this line. You indented it, but indention here makes no sense. Under what condition do you want to 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

Well, with your current code, if you do not create a new customer first (option 1), then options 2, 3, and 4 will result in errors since those options reference properties the Customer class initiates. I would not even create obj unless option 1 has been selected. Then, I would not allow options 2, 3, or 4 to be selected if no obj has been created.

Since your other code assumes you would only ever have one customer anyway, I would not even have an option 1 and instead would ask the user to enter the name, account number, account type, and amount when the program starts. You would then use this information to create the obj and use getData method to give it the property values it needs for options 3 - 4.

If you need to be able to create multiple customers, then you will have to completely rewrite your entire program because the current structure assumes there will only be one customer.

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

Since it is for a single customer, I would defintely change the code to ask the user to input the account information when the app first loads. It makes no sense to have a New Customer option for a single user unless you are not going to show the other options (Deposit, Withdrawal, Display) until after the user info has been entered and then hide the New Customer and display the other 3 options after the user as successfully entered the new customer account info.

If you leave the New Customer option with the other options, then you will need to make sure to not allow a user to select Deposit, Withdrawal, or Display until the new account has been set up.

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