We need to add a new function to the Flight class that calculates the flight time based on the distance and average cruising speed of the aircraft. This will enhance the existing functionality by providing users with an estimate of how long their flight will take.
Steps:
- Update the
Flight class in flight.py to include a new method calculate_flight_time.
- Use the average cruising speed of the aircraft (e.g., 840 km/h for Boeing 737-800) to calculate the flight time.
- Include this new calculation in the
print_flight_params and _to_dict methods to display and save the flight time.
Example Implementation:
class Flight:
# ... existing code ...
def calculate_flight_time(self) -> float:
average_speed_kmph = self.aircraft_data["Speed"]["Cruise"]
if average_speed_kmph <= 0:
raise ValueError("Invalid aircraft speed data.")
flight_time_hours = self.distance_km / average_speed_kmph
return flight_time_hours
def print_flight_params(self) -> None:
flight_time = self.calculate_flight_time()
print(
# ... existing code ...
f"\nFlight Time: {flight_time:.2f} hours\n"
)
def _to_dict(self) -> dict[str, Any]:
flight_dict = {
# ... existing code ...
"parameters": {
# ... existing code ...
"flight_time_hours": self.calculate_flight_time()
}
}
return flight_dict
We need to add a new function to the
Flightclass that calculates the flight time based on the distance and average cruising speed of the aircraft. This will enhance the existing functionality by providing users with an estimate of how long their flight will take.Steps:
Flightclass inflight.pyto include a new methodcalculate_flight_time.print_flight_paramsand_to_dictmethods to display and save the flight time.Example Implementation: