why these variables in def number 3 are not defined?

Question:

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}')
Asked By: Solari

||

Answers:

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

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 initializer __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
Answered By: constantstranger
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.