-2

Whenever I run my code I get: NameError: name 'Object_Oriented_Programming' is not defined

How do I define the name Object_Oriented_Programming?

Code:

class Object_Oriented_Programming:
    
    class Inheritance():
        def __init__(self, name, age):
            self.name = name
            self.age = age

        class SchoolMember():
            '''Represents any school member.'''

            def __init__(self, name, age):
                self.name = name
                self.age = age
                print('(Initialized SchoolMember: {})'.format(self.name))

            def tell(self):
                '''Tell my details.'''
                print('Name:"{}" Age:"{}"'.format(
                    self.name, self.age), end=" ")

        class Teacher(SchoolMember):
            '''Represents a teacher.'''

            def __init__(self, name, age, salary):
                Object_Oriented_Programming.Inheritance.SchoolMember.__init__(
                    self, name, age)
                self.salary = salary
                print('(Initialized Teacher: {})'.format(self.name))

            def tell(self):
                Object_Oriented_Programming.Inheritance.SchoolMember.tell(self)
                print('Salary: "{:d}"'.format(self.salary))
Cyanomous
  • 1
  • 1
  • 4
  • 4
    Your first two classes are redundant. Just use SchoolMember onwards. And then Teacher will just need 'super()' – quamrana Feb 21 '22 at 17:15
  • Does this answer your question? [How to refer to the class from within it (like a recursive function)](https://stackoverflow.com/questions/2035423/how-to-refer-to-the-class-from-within-it-like-a-recursive-function) – Tomerikoo Feb 21 '22 at 17:16
  • Yup, it does answer my question. Thanks! – Cyanomous Feb 21 '22 at 17:27
  • Apart from this nesting being very weird and I can't understand why it's necessary, doing `super()` instead of `Object_Oriented_Programming.Inheritance.SchoolMember` would probably make it work – Tomerikoo Feb 21 '22 at 19:10

2 Answers2

0

As far as I am concerned about the code, I ran it on the terminal and there wasn't any issue, it simple executed and nothing showed up as nothing was printed.

Now, If you really want to know how to define an object in oops in Python, here is the way to do it:

  1. First instead of creating a class inside a class, you should just create one class and then write __init__ method including all things such as name or age.
  2. When do with this, you can then make a new variable at the end and put that = the class name (the name, age). for example, I have attached an img to show you a piece of code.

Code

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Sagar Rai
  • 7
  • 3
  • Note: By mistake I have written __init__ is a method , sorry , its a constructor !! – Sagar Rai Feb 21 '22 at 17:42
  • 1
    Please avoid posting images (or worse, links to images) of code or errors. Anything text-based (code and errors) should be posted as text directly in the question itself and formatted properly as a [mre]. You can get more [formatting help here](https://stackoverflow.com/help/formatting). You can also read about [why you shouldn't post images/links of code](//meta.stackoverflow.com/q/285551). – Tomerikoo Feb 21 '22 at 19:09
  • 1
    First of all, `__init__` is indeed a method. It just happens to *also* be a constructor. Second, you should just [edit] your post if you want to fix something... – Tomerikoo Feb 21 '22 at 19:12
  • Yeah , my mistake , Btw did you get your problem solved?? – Sagar Rai Feb 22 '22 at 04:33
  • It is not my question, and you still didn't post the code as text... – Tomerikoo Feb 22 '22 at 09:46
0

code :

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
Sagar Rai
  • 7
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 22 '22 at 13:16