Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Car update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Car(Vehicle):
def __init__(self, brand, model, year, seats, fuel_type):
super().__init__(brand, model, year)
self.seats = seats # Number of seats in the car
self.fuel_type = fuel_type # Type of fuel (e.g., petrol, diesel, electric)

def display_info(self):
"""Override the parent class method for a Car."""
return f"Car: {self._brand} {self._model} ({self._year}), Seats: {self.seats}, Fuel: {self.fuel_type}"

def play_music(self):
"""Simulates playing music in the car."""
return "Playing your favorite music playlist!"

def activate_air_conditioner(self):
"""Simulates turning on the air conditioner."""
return "Air conditioner activated. Enjoy your ride!"

def check_fuel(self):
"""Checks the fuel type and returns a message."""
if self.fuel_type.lower() == "electric":
return "The car is fully charged."
else:
return f"Fuel type: {self.fuel_type}. Please ensure the tank is full."
Empty file added Child class.ipynb
Empty file.
26 changes: 24 additions & 2 deletions Truck.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from Vehicle import Vehicle

class Truck(Vehicle):
def __init__(self, brand, model, year, max_load):
super().__init__(brand, model, year)
Expand All @@ -12,3 +10,27 @@ def display_info(self):
def honk(self):
"""Specific implementation for a truck."""
return "HOOOOONK!"

def load_cargo(self, weight):
"""
Simulates loading cargo onto the truck.
:param weight: Weight of the cargo in tons.
:return: Message indicating whether the cargo was successfully loaded.
"""
if weight <= self.max_load:
return f"Successfully loaded {weight} tons of cargo."
else:
return f"Cannot load {weight} tons. Exceeds max load of {self.max_load} tons."

def calculate_mileage(self, fuel, distance):
"""
Calculates mileage based on fuel consumption and distance traveled.
:param fuel: Amount of fuel consumed in liters.
:param distance: Distance traveled in kilometers.
:return: Mileage (km/l).
"""
if fuel > 0:
mileage = distance / fuel
return f"The truck's mileage is {mileage:.2f} km/l."
else:
return "Fuel consumption must be greater than 0 to calculate mileage."
24 changes: 24 additions & 0 deletions Updated car class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Car(Vehicle):
def __init__(self, brand, model, year, seats, fuel_type):
super().__init__(brand, model, year)
self.seats = seats # Number of seats in the car
self.fuel_type = fuel_type # Type of fuel (e.g., petrol, diesel, electric)

def display_info(self):
"""Override the parent class method for a Car."""
return f"Car: {self._brand} {self._model} ({self._year}), Seats: {self.seats}, Fuel: {self.fuel_type}"

def play_music(self):
"""Simulates playing music in the car."""
return "Playing your favorite music playlist!"

def activate_air_conditioner(self):
"""Simulates turning on the air conditioner."""
return "Air conditioner activated. Enjoy your ride!"

def check_fuel(self):
"""Checks the fuel type and returns a message."""
if self.fuel_type.lower() == "electric":
return "The car is fully charged."
else:
return f"Fuel type: {self.fuel_type}. Please ensure the tank is full."
8 changes: 7 additions & 1 deletion Vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ def honk(self):
return "Beep beep!"

def fuel_up(self):
"""Simulates fueling the vehicle."""
return "Full tank"

def windshield_fluid(self):
return "Windshield fluid empty."

def brake(self):
print("Braking the car.")
"""Simulates braking."""
return "Braking the vehicle."

def mileage(self):
"""Provides general information about mileage."""
return "High mileage!"
30 changes: 26 additions & 4 deletions usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -27,6 +27,17 @@
"Meep meep!\n",
"HOOOOONK!\n"
]
},
{
"ename": "AttributeError",
"evalue": "'Truck' object has no attribute 'mileage'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[13], line 12\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(motorcycle\u001b[38;5;241m.\u001b[39mhonk())\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28mprint\u001b[39m(truck\u001b[38;5;241m.\u001b[39mhonk())\n\u001b[1;32m---> 12\u001b[0m \u001b[38;5;28mprint\u001b[39m(truck\u001b[38;5;241m.\u001b[39mmileage())\n",
"\u001b[1;31mAttributeError\u001b[0m: 'Truck' object has no attribute 'mileage'"
]
}
],
"source": [
Expand All @@ -40,8 +51,19 @@
"\n",
"print(car.honk())\n",
"print(motorcycle.honk())\n",
"print(truck.honk())"
"print(truck.honk())\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -60,7 +82,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down