-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession8.py
More file actions
237 lines (200 loc) · 7.25 KB
/
session8.py
File metadata and controls
237 lines (200 loc) · 7.25 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#فوتبال
def track_points(**players):
high_score = 0
champion = ""
for player, points in players.items():
print(f"{player} scored: {points}")
if points > high_score:
high_score = points
champion = player
return f"Champion is {champion} with {high_score} points!"
# Get player data from user
players = {}
num_players = int(input("How many players? "))
for i in range(num_players):
name = input(f"Enter player {i+1} name: ")
score = int(input(f"Enter {name}'s score: "))
players[name] = score
# Use the function with collected data
result = track_points(**players)
print(result)
class GameCharacter:
def __init__(self, name, character_type):
self.name = name
self.character_type = character_type
self.level = 1
self.health = 100
self.coins = 0
def collect_coins(self, amount):
self.coins += amount
return f"{self.name} collected {amount} coins! Total: {self.coins}"
def take_damage(self, damage):
self.health -= damage
if self.health < 0:
self.health = 0
return f"{self.name} took {damage} damage! Health: {self.health}"
def heal(self):
if self.coins >= 10:
self.coins -= 10
self.health = 100
return f"{self.name} is fully healed!"
return f"Not enough coins! Need 10 coins to heal."
def level_up(self):
self.level += 1
return f"Congratulations! {self.name} is now level {self.level}!"
def display_stats(self):
print(f"\nCharacter: {self.name}")
print(f"Type: {self.character_type}")
print(f"Level: {self.level}")
print(f"Health: {self.health}")
print(f"Coins: {self.coins}")
# Create a character
hero = GameCharacter("Thor", "Warrior")
# Play the game
print(hero.collect_coins(15))
print(hero.take_damage(30))
print(hero.heal())
print(hero.level_up())
hero.display_stats()
##########################################################################################3
#Employee Class
class Employee :
def __init__ (self,firstName,lastName,birthday,company,sallery,hiredYear):
self.firstName=firstName
self.lastName=lastName
self.birthday=birthday
self.company=company
self.sallery=sallery
self.hiredYear=hiredYear
def printInfo(self):
print(f"{self.firstName} {self.lastName} {self.birthday} {self.company} {self.sallery} {self.hiredYear}")
def getAge(self):
print(f"{1402-self.birthday}")
def getTotalSallery(self):
print(self.sallery * (1402-self.hiredYear) * 12)
def increaseSallery(self,value):
self.sallery=self.sallery + (value/100) * self.sallery
employee=Employee("Maziyar","kolagar",1000,"google",1383,1398)
employee.printInfo()
employee.getAge()
employee.getTotalSallery()
employee.increaseSallery(15)
#None
class ShoppingCart:
def __init__(self):
self.items = []
self.discount = None
self.last_item_added = None
def add_item(self, item):
self.items.append(item)
self.last_item_added = item
def set_discount(self, discount_code):
if discount_code == "SAVE10":
self.discount = 0.1
else:
self.discount = None
def get_last_added(self):
return self.last_item_added if self.last_item_added else "No items added yet"
# Create cart
cart = ShoppingCart()
# Check last added when empty
print(cart.get_last_added()) # Output: "No items added yet"
# Add items
cart.add_item("Book")
cart.add_item("Laptop")
print(cart.get_last_added()) # Output: "Laptop"
# Try discount
cart.set_discount("SAVE10")
print(cart.discount) # Output: 0.1
# Part 2: Inheritance And Encapsulation
# Basic inheritance
class Person:
def __init__(self, firstName, lastName, country, age, nc):
self.firstName = firstName
self.lastName = lastName
self.country = country
self.age = age
self.nc = nc
class Employee(Person):
def __init__(self, firstName, lastName, country, age, nc, salary, company):
super().__init__(firstName, lastName, country, age, nc)
self.salary = salary
self.company = company
# Test Person-Employee
employee = Employee("Amir", "Amiri", "Iran", 20, 205, 2000, "google")
print(employee.firstName)
# For Encapsulation, we use protected and private attributes
class BankAccount:
def __init__(self):
self._balance = 0 # Protected attribute
def get_balance(self):
return f"${self._balance}"
def deposit(self, amount):
if amount > 0:
self._balance += amount
return "Deposit successful"
return "Invalid amount"
def withdraw(self, amount):
if amount <= self._balance:
self._balance -= amount
return "Withdrawal successful"
return "Insufficient funds"
# Good way to use the account
account = BankAccount()
account.deposit(100)
print(account.get_balance()) # Shows $100
account.withdraw(50)
print(account.get_balance()) # Shows $50
# If we didn't use encapsulation, someone could do this:
# account._balance = -1000000 # This would be bad!
#SETTERS AND GETTERS:
#When you need to validate data before setting it
#When working with private or protected attributes
#When you need to perform additional operations during get/set
#This but with Setter and Getter:
class Person:
def __init__(self, firstName, lastName, country, age, nc):
self.firstName = firstName
self.lastName = lastName
self.country = country
self.age = age
self.nc = nc
def setfirstName(self, firstName):
if len(firstName) >= 2:
self.firstName = firstName
print("Name updated successfully")
else:
print("Name must be at least 2 characters")
def setage(self, age):
if 0 <= age <= 120:
self.age = age
print("Age updated successfully")
else:
print("Age must be between 0 and 120")
def getfirstName(self):
return self.firstName
def getage(self):
return self.age
class Employee(Person):
def __init__(self, firstName, lastName, country, age, nc, salary, company):
super().__init__(firstName, lastName, country, age, nc)
self.salary = salary
self.company = company
def setsalary(self, salary):
if salary >= 1000:
self.salary = salary
print("Salary updated successfully")
else:
print("Salary must be at least 1000")
def getsalary(self):
return self.salary
# Test
employee = Employee("Amir", "Amiri", "Iran", 20, 205, 2000, "google")
employee.setfirstName("A") # Will show error message
employee.setfirstName("Ali") # Will work
employee.setage(150) # Will show error message
employee.setage(25) # Will work
employee.setsalary(3000) # Will work
print(employee.getfirstName()) # Shows Ali
print(employee.getage()) # Shows 25
print(employee.getsalary()) # Shows 3000