-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarkey.py
More file actions
45 lines (40 loc) · 1.58 KB
/
Copy pathcarkey.py
File metadata and controls
45 lines (40 loc) · 1.58 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
class Car:
def __init__ (self, model, color):
self.model = model
self.color = color
self.__engine_running = False
self.__fuel_level = 100
def start_engine(self, car_key):
if car_key == "car_key_number":
self.__engine_running = True
print("Vroom! Engine started.")
def stop_engine(self):
self.__engine_running = False
print("Silence! Engine stopped.")
def drive(self, distance):
if self.__engine_running:
if self.__fuel_level > 0:
fuel_needed = distance / 10
if fuel_needed > self.__fuel_level:
# Can only drive part of the distance
possible_distance = self.__fuel_level * 10
print(f"Not enough fuel! Only drove {possible_distance} miles.")
self.__fuel_level = 0
else:
self.__fuel_level -= fuel_needed
print(f"Driving {distance} miles.")
print(f"Fuel level: {self.__fuel_level}%")
else:
print("Out of fuel! Need to refuel.")
else:
print("Engine is not running. Start the engine first.")
def refuel(self, amount):
self.__fuel_level += amount
if self.__fuel_level > 100:
self.__fuel_level = 100
print(f"Refueled. Fuel level: {self.__fuel_level}%")
my_car = Car("Toyota Camry", "Red")
print(f"My car is a {my_car.color} {my_car.model}.")
my_car.start_engine("car_key_number")
my_car.drive(1000)
my_car.stop_engine()