Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why these variables in def number 3 are not defined?

class Company:
    def __init__(self,name,age,salary,rating):
        self.Employee=name
        self.Employeeage =age
        self.Employeesalary =salary
        self.rating=rating
    def myfunc (self):
        if self.rating <= 2.5:
            print('BAD')
        else:
            print('GOOD')        
    def sfunc (self):
        if self.age <= 60: #here age is not defined
             self.salary+=5000 #here salary is not defined
             print(f'salary is {self.salary}')

>Solution :

You have asked why the variables in the method sfunc() (or "def number 3") are not defined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The things that are not defined (technically attributes from the class rather than "variables") are named age and salary. It looks like you attempted to initialize these in the constructor _init_() but used different names (Employeeage and Employeesalary).

Try this instead:

class Employee:
    def __init__(self,name,age,salary,rating):
        self.name=name
        self.age =age
        self.salary =salary
        self.rating=rating
    def myfunc (self):
        if self.rating <= 2.5:
            print('BAD')
        else:
            print('GOOD')        
    def sfunc (self):
        if self.age <= 60: #here age is not defined
             self.salary+=5000 #here salary is not defined
             print(f'salary is {self.salary}')

e = Employee('Brad Pitt',58,1000000,9.9)
e.myfunc()
e.sfunc()

Output:

GOOD
salary is 1005000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading