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 findreturnflight function did not return the correct Flight object or a ValueError when a flight is not found. and Your findflightbetween 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
allairports
allflights
def loaddataairportfile, flightfile:
try:
# Load airport data
with openairportfile, r as file:
for line in file:
data line.stripsplit
code datastrip
city datastrip
country datastrip
airport Airportcode city, country
allairportscode airport
printairport
# Load flight data
with openflightfile, r as file:
for line in file:
data line.stripsplit
printdata
fnumber data data
origin allairportsdatastrip
destination allairportsdatastrip
duration floatdatastrip
flight Flightfnumber origin, destination, duration
printflight
if origin.getcode not in allflights:
allflightsorigingetcode
allflightsorigingetcodeappendflight
return True
except Exception as error:
printfError loading data: error
return False
def getairportbycodecode:
if code in allairports:
return allairportscode
raise ValueErrorfNo airport with the given code: code
def findallcityflightscity:
flights
for flightlist in allflights.values:
for flight in flightlist:
originairport flight.getorigin
destairport flight.getdestination
if originairport.getcitylower city.lower or destairport.getcitylower city.lower:
flights.appendflight
return flights
def findallcountryflightscountry:
flights
for flightlist in allflights.values:
for flight in flightlist:
originairport flight.getorigin
destairport flight.getdestination
if originairport.getcountrylower country.lower or destairport.getcountrylower country.lower:
flights.appendflight
return flights
def findflightbetweenorigairport, destairport:
# Check for direct flight
if origairport.getcode in allflights:
for flight in allflightsorigairport.getcode:
if flight.getdestinationgetcode destairport.getcode:
return fDirect Flight: origairport.getcode to destairport.getcode
# Check for singlehop connection
connectingairports
if origairport.getcode in allflights:
for flight in allflightsorigairport.getcode:
if flightgetdestinationgetcode in allflights:
for flight in allflightsflightgetdestinationgetcode:
if flightgetdestinationgetcode destairport.getcode:
connectingairports.appendflightgetdestinationgetcode
if connectingairports:
return connectingairports
raise ValueErrorfThere are no direct or singlehop connecting flights from origairport.getcode to destairport.getcode
def shortestflightfromorigairport:
if origairport.getcode in allflights:
flights allflightsorigairport.getcode
shortestflight flights
for flight in flights:
if flight.getduration shortestflight.getduration:
shortestflight flight
return shortestflight
return None
def findreturnflightfirstflight, allflights:
# Get the origin and destination codes from the first flight
if allflights is None:
raise TypeErrorfindreturnflight requires an 'allflights' parameter"
# Ensure firstflight is a valid Flight object
if not isinstancefirstflight, Flight:
raise TypeErrorfirstflight must be a Flight object"
# Get origin and destination codes
origincode firstflight.getorigingetcode
destcode firstflight.getdestinationgetcode
# Search for the return flight
for flight in allflights:
if flightgetorigingetcode destcode and
flight.getdestinationgetcode origincode:
return flight
# Raise ValueError if no return flight is found
raise ValueErrorfThere is no flight from destcode to origincode
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
