-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.py
More file actions
62 lines (45 loc) · 1.52 KB
/
Copy pathconstructor.py
File metadata and controls
62 lines (45 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Employee:
# Create __init__() method
def __init__(self, name, salary=0 ):
# Create the name and salary attributes
self.name = name
self.salary = salary
# From the previous lesson
def give_raise(self, amount):
self.salary += amount
def monthly_salary(self):
return self.salary/12
emp = Employee("Korel Rossi")
print(emp.name)
print(emp.salary)
# 2 next question
class Employee:
def __init__(self, name, salary=0):
self.name = name
# Modify code below to check if salary is positive
if salary >0:
self.salary = salary
else:
self.salary = 0
print("Invalid salary! Setting salary to 0.")
# ...Other methods omitted for brevity ...
emp = Employee("Korel Rossi", -1000)
print(emp.name)
print(emp.salary)
initializing attributes in the constructor is a good idea, because this ensures that the object has all the necessary attributes the moment it is created.
# Import datetime from datetime
from datetime import datetime
class Employee:
def __init__(self, name, salary=0):
self.name = name
if salary > 0:
self.salary = salary
else:
self.salary = 0
print("Invalid salary!")
# Add the hire_date attribute and set it to today's date
self.hire_date= datetime.today()
# ...Other methods omitted for brevity ...
emp = Employee("Korel Rossi", -1000)
print(emp.name)
print(emp.salary)