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}')
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
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}')
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