Python Forum
cant able to make methods interact with each other in the class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cant able to make methods interact with each other in the class
I am trying to work on an ATM project for practice purpose, where its checks for the pin entered and if it is correct then shows options to deposit, check balance or transfer.
I am trying to implement it in one flow i mean if i run a class i should be able to all this by calling each other function inside the class.

In below code im have written the class
Atm ()
when i call this class, it asks for the pin and then calls the
which calls
then pin_Check calls
Here in my program i am getting this error:
NameError: name 'main' is not defined
How can i make my class's methods see and interact with other method of same class.

class Atm:
    balance = 10000  
    def __init__(self):
        print("PIN should be exactly 4 digit")
        self.pin1 = input("Please input Your PIN: ")
        self.pin2 = input("Re-Enter you PIN: ")
    def options():
        opt=input("Enter the options from 1-4 \n 1= Check saving balance \n 2= Deposit funds \n 3= Withdraw funds \n 4= Transfer funds \n")
    def pin_check(self):
        pin1 =  self.pin1
        pin2 =  self.pin2
        if((len(pin1)==4 or len(pin2)==4) and (pin1==pin2)):
            print('Pin OK')
            return a
            print('\n\n !!!!ALERT-INVALID PIN!!!!\n\n !! PIN SHOULD MATCH !! \n\n           &       \n \n PIN SHOULD BE 4 DIGITS\n !!NO CHARS ALLOWED!!')
    def main(self):
        return a

You're trying to call main before it is declared - remember the interpreter parses the file from top to bottom. Why are you trying to call it on line 9 anyway?

Also, your code doesn't really make a lot of sense:

1. Why is the function called main in the first place; that doesn't seem to be a particularly descriptive name.

2. the pin_check method will return None in the else branch. Is that what you intended? Also, why do you need the local variables pin1 and pin2? Obviously you have access to the member variables self.pin1 and self.pin2.

3. Generally, your classes shouldn't really be taking input from the console - it's not particularly good design. Why aren't you passing the values to the __init__ method?

4. options creates a local variable opt that just gets thrown away.

I'm sure there are more things to comment on, but this is all I noticed on a quick glance.
You get the error message because main is not defined. There is no function named main, nor is there a function named pin_check or options or even __init__. Classes don't have functions, they have methods, and calling a method requires that you specify a parent.

Atm.main is a method. It looks a lot like a function, but it us not. Because Atm.main is a method it is required to have a first argument of "self". "self" will be the instance of Atm which is calling the method. To call Atm.main() you'll first need an instance of Atm. Something like this:
atm = Atm()
"atm.main()" tells Python that you want to call a method named "main" which is defined for "atm". Python will first look at atm's instance variables for something named "main". This will fail, so Python next looks for "main" in atm's class, Atm. "main" is defined for Atm, so Python calls the method "main" and provides "atm" as the first argument; "Atm.main(atm)". It is because Python automatically provides the first argument that all methods must have a first arg of "self" or "cls" (cls is used for class methods, something you will need to learn about later).

That is the mechanics of why Python is complaining, but as ndc85430 mentions there is a more wrong with your code. __init__ is a special method that is automatically called when an instance of a class is created. Does it make sense to ask for a PIN when creating an ATM? I don't know about your bank, but my bank has one ATM machine that can be used multiple times throughout the day. They do not install a new ATM for each transaction. Your ATM should also allow for multiple transactions. EnterPin might be method of your new ATM that askes for and validates the PIN.

Possibly Related Threads…
Thread Author Replies Views Last Post
  access share attributed among several class methods drSlump 0 245 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 700 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Listing All Methods Of Associated With A Class JoeDainton123 3 1,043 May-10-2021, 01:46 AM
Last Post: deanhystad
  too many methods in class - redesign idea? Phaze90 3 1,187 Mar-05-2021, 09:01 PM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 1,050 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  Question about naming variables in class methods sShadowSerpent 1 956 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Interact with Python Mario456 1 861 Jan-31-2020, 07:14 PM
Last Post: Larz60+
  a class calling one of its own methods? Skaperen 1 939 Jul-21-2019, 04:43 AM
Last Post: Gribouillis
  Build class to make a Smart list - trying icm63 7 1,807 Mar-28-2019, 08:53 PM
Last Post: icm63
  How to open and interact with an app SheeppOSU 1 1,172 Feb-17-2019, 05:06 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020