-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_exercise_page173.py
More file actions
220 lines (158 loc) · 7.21 KB
/
python_exercise_page173.py
File metadata and controls
220 lines (158 loc) · 7.21 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
# esercizio numero 9.6 pagina 173
class Restaurant:
"""A simple model to represent restaurant attributes."""
def __init__(self, restaurant_name):
"""Initialize restaurant name and type attributes."""
self.restaurant_name = restaurant_name
def describe_restaurant(self):
"""Prints the restaurant name and type."""
print(f"The restaurant is called {self.restaurant_name}.")
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = f"{self.restaurant_name}"
return long_name.title()
def open_restaurant(self):
"""Prints that the restaurant is open."""
print(f"{self.restaurant_name} is now open!")
class IceCreamStand(Restaurant):
"""Represents aspects of a restaurant, specific to ice cream."""
def __init__(self, restaurant_name):
"""Initialize attributes of the parent class.
Then initialize attributes specific to the ice cream stand."""
super().__init__(restaurant_name)
self.flavors_list = ['chocolate', 'vanilla', 'nutella', 'almond', 'strawberry']
def display_flavors(self):
"""Print a statement describing ice cream flavors."""
print(f"{self.restaurant_name} offers the following ice cream flavors:")
for flavor in self.flavors_list:
print(f"- {flavor.title()}")
# Create an instance of IceCreamStand
my_ice_cream_stand = IceCreamStand('sweet scoops')
# Display the descriptive name and available flavors
print(my_ice_cream_stand.get_descriptive_name())
my_ice_cream_stand.display_flavors()
# esercizio numero 9.7 pagina 173
class User:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def get_descriptive_name(self):
long_name = f"{self.first_name} {self.last_name}"
def describe_user(self):
print(f"your first name is {self.first_name}.")
print(f"your last name is {self.last_name}.")
def greet_user(self):
print(f"Thank you for your time, {self.first_name}!")
class Admin(User):
"""Represent a special kind of user"""
def __init__(self, first_name, last_name):
"""Initialize attribiutes of the parent class.
Then initialize attributes specific to the Admin class"""
super().__init__(first_name, last_name)
self.privileges_list = ['Can add posts', 'can remove user', 'can delete posts', 'can close chat']
def show_privileges(self):
"""Print a statement defining all Admin privileges"""
print(f"{self.first_name} {self.last_name} has the following privileges: ")
for privilege in self.privileges_list:
print(f"- {privilege.title()}")
# create an instance for Admin
My_great_user = Admin('Lorenzo', 'bradanini')
# display the descriptive name and available privileges
print(My_great_user.get_descriptive_name())
My_great_user.show_privileges()
#esercizio numero 9.8
class User:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def get_descriptive_name(self):
return f"{self.first_name} {self.last_name}"
def describe_user(self):
print(f"Your first name is {self.first_name}.")
print(f"Your last name is {self.last_name}.")
def greet_user(self):
print(f"Thank you for your time, {self.first_name}!")
class Privileges:
def __init__(self, privileges):
self.privileges_list = privileges
def show_privileges(self):
print("User has the following privileges:")
for privilege in self.privileges_list:
print(f"- {privilege}")
class Admin(User):
def __init__(self, first_name, last_name, privileges):
super().__init__(first_name, last_name)
self.privileges = Privileges(privileges)
def show_privileges(self):
print(f"{self.get_descriptive_name()} has the following privileges:")
self.privileges.show_privileges()
# create an instance for Admin
my_great_user = Admin('Lorenzo', 'Bradanini', ['can remove people', 'can promote posts', 'can limitate user access'])
# display the descriptive name and available privileges
print(my_great_user.get_descriptive_name())
my_great_user.show_privileges()
# esercizio numero 9.9
class Car:
"""A simple attempt to represent a car."""
def __init__(self, make, model, year):
"""Initialize attributes to describe a car"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name"""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print a statement showing the car's mileage"""
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
"""Set the odometer reading to the given value"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self,miles):
"""Add the given amount to the odometer reading"""
self.odometer_reading += miles
class Battery:
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=40):
"""Initialize the battery's attributes."""
self.battery_size = battery_size
def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-KWh battery.")
def get_range(self):
"""Print a statement about the range this battery provides."""
if self.battery_size == 40:
range = 150
elif self.battery_size == 65:
range = 225
print(f"This car can go about {range} miles on a full charge.")
def upgrade_battery(self):
"""Upgrade the battery capacity to 65 KWh if it's not already."""
if self.battery_size <= 65:
print("Upgrading the battery capacity to 65 KWh.")
self.battery_size == 65
self.get_range()
else:
print("The battery capacity is already 65 KWh.")
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles"""
def __init__(self, make, model, year):
"""Initialize attributes of the parent class.
Then initialize attributes specific to an electric car."""
super().__init__(make, model, year)
self.battery = Battery()
# Create an instance of ElectricCar
my_leaf = ElectricCar('Nissan', 'Leaf', 2024)
# Display the descriptive name of the electric car
print(my_leaf.get_descriptive_name())
# Describe the battery of the electric car
my_leaf.battery.describe_battery()
# Display the range of the electric car
my_battery = Battery(65)
my_battery.describe_battery()
my_battery.upgrade_battery()