import datetime
class Employee:
def __init__ (self,first,last,pay,dob):
self.first = first
self.last = last
self.pay = pay
self.email = first + "." + last + "#company.com"
self.dob = dob
def fullname(self):
return '{} {}'.format(self.first,self.last)
def age(dob):
today = date.today()
return today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))
emp1 = Employee("aa","ss",122,date(1991,2,3))
emp2 = Employee("ww","ii",637,date(1997,8,24))
emp3 = Employee("ee","oo",986,date(1986,10,19))
#driver code
print(Employee.age(emp2))
I am getting the below error:
AttributeError: 'Employee' object has no attribute 'year'
What is incorrect in this?
Your Employee.age() method signature has only one argument - dob. When you call it first argument that is passed is the instance. By convention we use argument self for it. But you use dob. Stick to convention as you do in the other method. then work with self.dob.year
from datetime import date
class Employee:
def __init__ (self,first,last,pay,dob):
self.first = first
self.last = last
self.pay = pay
self.email = first + "." + last + "#company.com"
self.dob = dob
#property
def fullname(self):
return '{} {}'.format(self.first, self.last) # in 3.6 return f'{self.first} {self.last}'
def age(self):
today = date.today()
return today.year - self.dob.year - ((today.month, today.day) < (self.dob.month, self.dob.day))
emp1 = Employee("aa","ss",122,date(1991,2,3))
emp2 = Employee("ww","ii",637,date(1997,8,24))
emp3 = Employee("ee","oo",986,date(1986,10,19))
#driver code
print(emp1.age())
Note, I also added #property decorator, so that fullname is "read-only" property
and also fixed the import
in 3.6+ you can use f-strings. age and e-mail property, like fullname may be read-only property (i.e. you should not specify age that contradict to dob), e.g.
#property
def age(self):
today = date.today()
return today.year - self.dob.year - ((today.month, today.day) < (self.dob.month, self.dob.day))
Then you can use it like this
print(emp1.age) # note the difference when it is method in the original class, and now when its property
Your employee class does not have a year attribute
but
employee.dob has that
import datetime
from datetime import date
class Employee:
def __init__ (self,first,last,pay,dob):
self.first = first
self.last = last
self.pay = pay
self.email = first + "." + last + "#company.com"
self.dob = dob
def fullname(self):
return '{} {}'.format(self.first,self.last)
def age(dob):
today = date.today()
dob = dob.dob # get dob from the object(of employee class) <<<<< Single change
return today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))
emp1 = Employee("aa","ss",122,date(1991,2,3))
emp2 = Employee("ww","ii",637,date(1997,8,24))
emp3 = Employee("ee","oo",986,date(1986,10,19))
#driver code
print(Employee.age(emp2))
Related
class Employee:
company = 'Google'
def __init__(self, name, salaryInput, salIncrement):
self.name = name
self.salaryInput = salaryInput
self.salIncrement = salIncrement
def salary(self):
print('Base salary of {} is ${}'.format(self.name, self.salary))
def increment(self):
print('Increment in salary = ${}'.format(self.salIncrement))
#property
def salaryAfterIncrement(self):
return self.salaryInput + self.salIncrement
#salaryAfterIncrement.setter
def salaryAfterIncrement(self, salaryInput):
self.increment = salaryAfterIncrement - self.salaryInput
abhishek = Employee('Abhishek', 100, 50)
print(abhishek.salaryAfterIncrement)
print(abhishek.increment)
You need to add parenthesis. And BTW, you need to use return in .increment() function. This wouldn't solve your problem but it will print a None. So try -
return 'Increment in salary = ${}'.format(self.salIncrement)
Then use print -
print(abhishek.increment())
Or if you do not want to use return then call the function without print statement -
print('Increment in salary = ${}'.format(self.salIncrement))
Then call the function -
abhishek.increment()
class Employee(object):
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
def pay_raise(self):
self.pay = int(self.pay * 1.04)
emp_1 = Employee("Mark", "Johnson", 50000)
emp_1.pay_raise()
When I write that emp_1.pay_raise() phrase or emp_1.fullname() i dont get any results or any errors either after pressing "run" or "debug" in pycharm. Can you notice any mistakes in my code? I will very appereciate.
You are not printing out any of the "results" from executing the methods. Try this:
class Employee(object):
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
def pay_raise(self):
self.pay = int(self.pay * 1.04)
emp_1 = Employee("Mark", "Johnson", 50000)
print(emp_1.fullname())
emp_1.pay_raise()
print(emp_1.pay)
I was trying to add the parameter bonus (which will take an integer) with the instance variable self.pay and wanted to print that final payment with the worker's name. But, I could not print that added total payment
I want to call the method rise() instead of returning anything from it, but I am confused how I can call that and pass an integer number.
class Information:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def rise(self,int(bonus)):
self.pay = self.pay + bonus
def __str__(self):
return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)
if __name__ == "__main__":
emp1 = Information("tom","jerry",999)
print (emp1)
class Information:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def raise_salary(self, bonus):
self.pay += int(bonus) # exception if bonus cannot be casted
def __str__(self):
return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)
if __name__ == "__main__":
emp1 = Information("tom", "jerry", 999)
print(emp1)
emp1.raise_salary('1000') # or just emp1.raise(1000)
print(emp1)
I tried with below code.
I updated def rise(self,int(bonus)): to def rise(self,bonus):
class Information:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def rise(self,bonus):
self.pay = self.pay + bonus
def __str__(self):
return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)
if __name__ == "__main__":
emp1 = Information("tom","jerry",999)
emp1.rise(89)
print (emp1)
Here's the simple python 3 object code from the web that is not platform dependent.. I cannot get working
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{}{}'.format(self.first, self.last)
emp_1 = Employee('John','Doe','80000')
emp_2 = Employee('Jane','Foo','90000')
emp_2.fullname()
print (Employee.fullname(emp_1))
print (emp_2.fullname())
The error I get is as follows:
NameError Traceback (most recent call last)
in ()
----> 1 class Employee:
2
3 def init(self, first, last, pay):
4 self.first = first
5 self.last = last
in Employee()
10 return '{}{}'.format(self.first, self.last)
11
---> 12 emp_1 = Employee('John','Doe','80000')
13 emp_2 = Employee('Jane','Foo','90000')
14
NameError: name 'Employee' is not defined
Indentation is crucial in Python. Try the below code.
Your class instances must be defined outside the class itself. This is recognised by there being no indentation for definitions of emp_1 and emp_2.
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{}{}'.format(self.first, self.last)
emp_1 = Employee('John','Doe','80000')
emp_2 = Employee('Jane','Foo','90000')
emp_2.fullname()
print(Employee.fullname(emp_1))
print(emp_2.fullname())
This is simply an indentation error.
Python defines scopes like classes, methods and other blocks by indentation. Usually 4 spaces are used.
Since you put your instantiation of emp_1 and emp_2 with the same indentation as the class's methods they are literally part of the class.
What you probably meant was:
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{}{}'.format(self.first, self.last)
emp_1 = Employee('John','Doe','80000')
emp_2 = Employee('Jane','Foo','90000')
emp_2.fullname()
print (Employee.fullname(emp_1))
print (emp_2.fullname())
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{}{}'.format(self.first, self.last)
def main():
emp_1 = Employee('John','Doe','80000')
emp_2 = Employee('Jane','Foo','90000')
emp_2.fullname()
print (Employee.fullname(emp_1))
print (emp_2.fullname())
if __name__ == '__main__':
main()
How can I use today and returntime in return_fee function?
import datetime
class Movie(object):
def __init__(self,title):
self.title = title
def time_of_return(self):
self.today = today
self.returntime = returntime
today = datetime.datetime.now()
returntime = today + datetime.timedelta(days=30)
def return_fee(Movie):
fee = -2
delta = today - returntime
I would do it like this:
class Movie(object):
def __init__(self,title):
self.title = title
def get_times(self):
now = datetime.datetime.now()
return now, now + datetime.timedelta(days=30)
def time_of_return(self):
now, returntime = self.get_times()
return returntime
def return_fee(self):
fee = -2
now, returntime = self.get_times()
delta = now - returntime
return <whatever based on fee and delta>
If you want time_of_return and return_fee to be instance attributes, call time_of_return from __init__ to set them and then prefix with self:
class Movie(object):
def __init__(self,title):
self.title = title
self.time_of_return()
def time_of_return(self):
self.today = datetime.datetime.now()
self.returntime = today + datetime.timedelta(days=30)
def return_fee(Movie):
fee = None
delta = self.today - self.returntime
# ... presumably do something else
Alternatively (since, in particular, today may change over time), call the function time_of_return from within return_fee and make sure it returns something:
class Movie(object):
def __init__(self,title):
self.title = title
def time_of_return(self):
today = datetime.datetime.now()
returntime = today + datetime.timedelta(days=30)
return today, returntime
def return_fee(Movie):
fee = None
today, returntime = self.time_of_return()
delta = today - returntime
# ... presumably do something else
It's a good idea to indent your code by 4 spaces, by the way. And None (or 0) would be a better default value for fee.
class Movie(object):
def __init__(self,title,today,returntime):#<----
self.title = title
def time_of_return(self):
self.today = today
self.returntime = returntime
today = datetime.datetime.now()
returntime = today + datetime.timedelta(days=30)
def return_fee(Movie):
fee = -2
delta = today - returntime
That's because __init__() is taking arguments that using for class from outside.But, when you use your Movie class, you have to define that arguments.