Taking a vacation: 3. Getting there


#1



https://www.codecademy.com/courses/python-beginner-P5YtY/1/2?curriculum_id=4f89dab3d788890003000096


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!


`python


#2

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


#3

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


#4

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
    }
    try:
        return cities[city]
    except KeyError:
        return 0

#5

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


#6

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