0

I am new to Python and know almost nothing. I am trying to interlink the functions to the constructor but am unable to. How to call the first 2 functions in a main function and the main function in constructor?

I could execute the code without _init_() and exec() by calling inpVal() and check() outside after creating object, but it is not showing any output when I am doing it in this way.

class Acute:

    def _init_(self):
        self.exec()

    def inpVal(self):
        print("Enter 3 positive integers(angles):")
        self.ang1 = int(input())
        self.ang2 = int(input())
        self.ang3 = int(input())

    def check(self):
        if self.ang1 <=0 or self.ang2 <=0 or self.ang3 <=0:
            print("Invalid Input")
        elif self.ang1+self.ang2+self.ang3 != 180:
            print("Not a Triangle")
        elif self.ang1 >=90 or self.ang2 >=90 or self.ang3 >=90:
            print("Not an Acute Triangle")
        else:
            print("Acute Triangle")

    def exec(self):
        self.inpVal()
        self.check()

t1 = Acute()

I want that only an object creation statement will be there after the class declaration and the constructor will be called by default and from it the exec function will be called which will call the input and check functions.

Rohan Nag
  • 3
  • 3
  • 2
    Possible typo: should be `__init__(self)` (double underscores both front and back). – TrebledJ May 24 '19 at 09:23
  • No. It is a special function in python classes. – Rohan Nag May 24 '19 at 09:27
  • So... um... why doesn't `def __init__` work for you? The "object creation statement will be there" and "will be called by default", which is what you're asking for, no? Correct me if I'm wrong, as I have a little trouble comprehending the last paragraph. – TrebledJ May 24 '19 at 09:35
  • I didn't understand your first comment... I did use double underscores. But I have to recheck. Thank you though. – Rohan Nag May 25 '19 at 00:47
  • Thank you... The problem was actually that I did not give double underscores. – Rohan Nag May 25 '19 at 01:59

3 Answers3

2

The following problems exist with your code:

  • _init_ is not correct name for python initializer function which is called when an object is created.. It should be __init__. Since, you have not given that, a default initializing function is created and that gets called.
  • exec is an existing python function name. So, it needs to be avoided.

Please see the modified code:

class Acute:
    def __init__(self):
        self.exec_custom()

    def inpVal(self):
        print("Enter 3 positive integers(angles):")
        self.ang1 = int(input())
        self.ang2 = int(input())
        self.ang3 = int(input())

    def check(self):
        if self.ang1 <=0 or self.ang2 <=0 or self.ang3 <=0:
            print("Invalid Input")
        elif self.ang1+self.ang2+self.ang3 != 180:
            print("Not a Triangle")
        elif self.ang1 >=90 or self.ang2 >=90 or self.ang3 >=90:
            print("Not an Acute Triangle")
        else:
            print("Acute Triangle")

    def exec_custom(self):
        self.inpVal()
        self.check()

t1 = Acute()

Output:

Enter 3 positive integers(angles):
1
2
3
Not a Triangle
Jay
  • 24,173
  • 25
  • 93
  • 141
  • 1
    On a pedantic note, `__init__` isn't a constructor either. (See [Python constructors and __init__](https://stackoverflow.com/questions/8985806/python-constructors-and-init).) – TrebledJ May 24 '19 at 09:38
0

How to call the first 2 functions in a main function and the main function in constructor?

You don't call main function in constructor. By definition main function is this is the entrance point of your script.

Inside __main__ module you should create your class.

prosti
  • 42,291
  • 14
  • 186
  • 151
0

The following code works as expected:

class Acute:

    def __init__(self):
        self.exec_method()
        self.ang1 = None
        self.ang2 = None
        self.ang3 = None

    def inp_val(self):
        print("Enter 3 positive integers(angles):")
        self.ang1 = int(input())
        self.ang2 = int(input())
        self.ang3 = int(input())

    def check(self):
        if self.ang1 <= 0 or self.ang2 <= 0 or self.ang3 <= 0:
            print("Invalid Input")
        elif self.ang1+self.ang2+self.ang3 != 180:
            print("Not a Triangle")
        elif self.ang1 >= 90 or self.ang2 >= 90 or self.ang3 >= 90:
            print("Not an Acute Triangle")
        else:
            print("Acute Triangle")

    def exec_method(self):
        self.inp_val()
        self.check()


t1 = Acute()

Output:

>>>python test.py 
Enter 3 positive integers(angles):
12
32
43
Not a Triangle
  • You need to use __init__ (double underscores)
  • You need to define the instance attributes in __init__. (Variables with self.prefix)
  • You cannot use built-in name. (exec) You can find the Built-in functions on this link: https://docs.python.org/3.2/library/functions.html
milanbalazs
  • 4,811
  • 4
  • 23
  • 45