0

I am new to python. I try to access the attribute acnt_amount from the class bank_Customer, but throws "AttributeError" error. How to access the attribute of the function getDetails to withdraw with in the class from one function to another function? What is the mistake that i do? Any help will be much appreciated! Thanks in advance!

Code:

class bank_Customer:

    def getDetails(self, cname, acnt_no, acnt_type, acnt_amount):
        self.cname = cname
        self.acnt_no = acnt_no
        self.acnt_type = acnt_type
        self.acnt_amount = acnt_amount
        row = self.cname + "," + str(self.acnt_no) + "," + self.acnt_type + "," + str(self.acnt_amount) + "\n"
        file = open('cust_details.csv','a')
        file.write(str(row))
        file.close()
        print('*'*40)
        print("Account has been added successfully!")
        return self.acnt_amount

    def withdraw(self):
        cash = int(input("Please enter the amount to be withdrawn: "))
        self.acnt_amount = self.acnt_amount - cash
        f"balance amount is {balance}"
        return balance

base = bank_Customer()
base.withdraw()

Error:

Traceback (most recent call last):
  File "C:\Users\kisha\IdeaProjects\Git projects in python\ATM application.py", line 96, in <module>
    base.withdraw()
  File "C:\Users\kisha\IdeaProjects\Git projects in python\ATM application.py", line 66, in withdraw
    self.acnt_amount = self.acnt_amount - cash
AttributeError: 'bank_Customer' object has no attribute 'acnt_amount'
1
  • 4
    At what point are you initialising self.acnt_amount to any value before trying to do math with it…? Commented Jan 13, 2023 at 10:50

3 Answers 3

1

As suggested, an init is required. Also consider setting up some defaults, and look at the use of "getters and setters". You may gain some insight from the following sample bank account class that I wrote some time ago as an example.

# @Author:srattigan
# @Date:2020-12-10 11:10:33
# @LastModifiedBy:srattigan
# @Last Modified time:2020-12-14 09:50:13
# demo class for inheritance

class BankAccount:
    """Generic Bank Account
    """
    acc_num = 100000

    def __init__(self, name, balance=0):
        self.name = name
        self.balance = balance
        self.account_num = self.acc_num
        BankAccount.acc_num += 1

    def deposit(self, amount):
        assert amount > 0, ValueError("Must deposit a positive amount")
        self.balance += amount
    
    def withdraw(self, amount):
        self.balance -= amount

    def __str__(self):
        rep = f"Bankaccount for {self.name}"
        rep += f"\n\tAcc Num: {self.account_num}"
        rep += f"\n\tBalance: €{self.balance:.2f}"
        return rep

customer = BankAccount("Fred Jones", 99)
print(customer)
Sign up to request clarification or add additional context in comments.

Comments

1

You need to def init as your first method. Thats where you specify cname, acnt_no etc.

Comments

1

you need to declare the variables inside the class first. create the init() constructor to declare and initialize those variables

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.