Taking a vacation: 3. Getting there



def hotel_cost(nights):
#hotel costs $140 per night
return 140*nights
def plane_ride_cost(city):
if city == "Charlotte":
return "183"
elif city == "Tampa":
return "220"
elif city == "Pittsburgh":
return "222"
elif city == "Los Angeles":
return "475"

Receiving "oops, try again. It looks like plane_ride_cost does not return 183 when city is Charlotte." No idea what's wrong with the code...

Thanks in advance for any help!



The return values are supposed to be numbers, not strings. Remove the quotes.


You also need to make an else statement at the end, you should never end it with elif.


Not so. There is no imperative to write an else statement. If none of the branches are followed the function will return None. Perfectly legitimate.

What I find interesting is that nobody has suggested using a dictionary for look-ups, as opposed to using control flow logic.

def plane_ride_cost(city):
    cities = {
        "Charlotte": 183,
        "Tampa": 220,
        "Pittsburgh": 222,
        "Los Angeles": 475
        return cities[city]
    except KeyError:
        return 0


Thank you both, for the replies I appreciate it a lot.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.