Question: make it like this and sure it passes Your find _ return _ flight function did not return the correct Flight object or a ValueError

make it like this and sure it passes Your find_return_flight function did not return the correct Flight object or a ValueError when a flight is not found. and Your find_flight_between function did not return the correct set or the string created by your Flight class's __str__ is incorrect.
from Flight import *
from Airport import *
# Containers to store all Airport and Flight objects
all_airports ={}
all_flights ={}
def load_data(airport_file, flight_file):
try:
# Load airport data
with open(airport_file, 'r') as file:
for line in file:
data = line.strip().split('-')
code = data[0].strip()
city = data[2].strip()
country = data[1].strip()
airport = Airport(code, city, country)
all_airports[code]= airport
print(airport)
# Load flight data
with open(flight_file, 'r') as file:
for line in file:
data = line.strip().split('-')
print(data)
fnumber= data[0]+"-"+ data[1]
origin = all_airports[data[2].strip()]
destination = all_airports[data[3].strip()]
duration = float(data[4].strip())
flight = Flight(fnumber, origin, destination, duration)
print(flight)
if origin.get_code() not in all_flights:
all_flights[origin.get_code()]=[]
all_flights[origin.get_code()].append(flight)
return True
except Exception as error:
print(f"Error loading data: {error}")
return False
def get_airport_by_code(code):
if code in all_airports:
return all_airports[code]
raise ValueError(f"No airport with the given code: {code}")
def find_all_city_flights(city):
flights =[]
for flight_list in all_flights.values():
for flight in flight_list:
origin_airport = flight.get_origin()
dest_airport = flight.get_destination()
if origin_airport.get_city().lower()== city.lower() or dest_airport.get_city().lower()== city.lower():
flights.append(flight)
return flights
def find_all_country_flights(country):
flights =[]
for flight_list in all_flights.values():
for flight in flight_list:
origin_airport = flight.get_origin()
dest_airport = flight.get_destination()
if origin_airport.get_country().lower()== country.lower() or dest_airport.get_country().lower()== country.lower():
flights.append(flight)
return flights
def find_flight_between(orig_airport, dest_airport):
# Check for direct flight
if orig_airport.get_code() in all_flights:
for flight in all_flights[orig_airport.get_code()]:
if flight.get_destination().get_code()== dest_airport.get_code():
return f"Direct Flight: {orig_airport.get_code()} to {dest_airport.get_code()}"
# Check for single-hop connection
connecting_airports =[]
if orig_airport.get_code() in all_flights:
for flight1 in all_flights[orig_airport.get_code()]:
if flight1.get_destination().get_code() in all_flights:
for flight2 in all_flights[flight1.get_destination().get_code()]:
if flight2.get_destination().get_code()== dest_airport.get_code():
connecting_airports.append(flight1.get_destination().get_code())
if connecting_airports:
return connecting_airports
raise ValueError(f"There are no direct or single-hop connecting flights from {orig_airport.get_code()} to {dest_airport.get_code()}")
def shortest_flight_from(orig_airport):
if orig_airport.get_code() in all_flights:
flights = all_flights[orig_airport.get_code()]
shortest_flight = flights[0]
for flight in flights:
if flight.get_duration()< shortest_flight.get_duration():
shortest_flight = flight
return shortest_flight
return None
def find_return_flight(first_flight, all_flights):
# Get the origin and destination codes from the first flight
if all_flights is None:
raise TypeError("find_return_flight() requires an 'all_flights' parameter")
# Ensure first_flight is a valid Flight object
if not isinstance(first_flight, Flight):
raise TypeError("first_flight must be a Flight object")
# Get origin and destination codes
origin_code = first_flight.get_origin().get_code()
dest_code = first_flight.get_destination().get_code()
# Search for the return flight
for flight in all_flights:
if (flight.get_origin().get_code()== dest_code and
flight.get_destination().get_code()== origin_code):
return flight
# Raise ValueError if no return flight is found
raise ValueError(f"There is no flight from {dest_code} to {origin_code}")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!