Click here to Skip to main content
15,358,692 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have experienced difficulties in my code, I tried to fix and double possible error I may counter it shows "AttributeError: 'Atm' object has no attribute 'getOwnerName' I don't know what I am missing. Can you please help out. Thank you

class Atm:
  def __init__(self,ownerName="",bal=500):
      self.__ownerName=ownerName
      self.__money=bal
  def setOwnerName(self,ownerName):
      self.__ownerName=ownerName
  def setBal(self,bal):
      def getOwnerName(self):
          return self.__ownerName
  def getBal(self):
      return self.__money
if __name__=='__main__':
        
        owner1,owner2 = Atm(),Atm()
        owner1.setOwnerName("Adam")
        owner2.setOwnerName("Ben")
        owner1.setBal(7000)
        owner2.setBal(10000)
        print(f'{owner1.getOwnerName()}money balance is:{owner1.getBal()}')
        print(f'{owner2.getOwnerName()}money balance is:{owner2.getBal()}')


What I have tried:

I tried fixing the string and the class that I call out, it still showing error
Posted
Updated 25-May-22 5:29am

Quote:
Python
...
  def setBal(self,bal):
      def getOwnerName(self):
          return self.__ownerName
Indentation is everything in Python. Your getOwnerName function is nested inside your setBal function. You need to fix that.

Python
...
  def setBal(self,bal):
    self.__money=bal
  def getOwnerName(self):
    return self.__ownerName
   
v2
Comments
javadoodles 25-May-22 22:00pm
   
The correction is helpful. Thank you so much for the help.
Look at your code, you have declared getOwnerName inside the setBal function. So setBal does nothing, and getOwnerName is hidden.
Python
def setBal(self,bal):
    def getOwnerName(self):
        return self.__ownerName
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900