plane_ride_cost('Charlotte') returned None instead of the correct value 183


#1

This is the error:
plane_ride_cost('Charlotte') returned None instead of the correct value 183

This is my code:

Vacation Trip Spending Estimator

Cost of Hotel Stay

print "Nyce Travel Agency Trip Spending Estimator"
nights = int(raw_input("How many night will you being staying? "))

def hotel_cost(nights):
return 140 * nights

Cost of plane

print "Plane Ticket Prices"
city = raw_input("What city are you visiting? ")

def plane_ride_cost(city):
if type(city) == str:
return
if city == "Charlotte":
return 183
if city == "Tampa":
return 220
if city == "Pittsburgh":
return 222
if city == "Los Angeles":
return 475
else:
return "City not found"

Cost of Rental Car

days = raw_input("How many days will you need a rental car? ")
def rental_car_cost(days):
cost = days * 40
if type(days) == int:
return rental_car_cost2(days)
def rental_car_cost2(days):
cost = days * 40
if days >= 7:
return (cost) - 50
elif days >= 3:
return (cost) - 20
else:
return (cost)

def trip_cost(city, days, nights):
return sum(rental_car_cost(days), hotel_cost(nights), plane_ride_cost(city))


#2

@cnyce305

I personally think they messed up by asking us to create the function with a ton of if/else statements to find out city when we have already learned a better way.

I should also mention to you that

if type(city) == str:
        return

Will not allow your function to work properly!

return is like break for a function except that it gives a value back.

Here is the best way to truly do something like what we need, unlike our if/else stuff we can scale this, we can add new entries and not have to worry about getting dumb errors.

Example:

city_prices = {
    "Charlotte": 183,
    "Pittsburgh": 222,
    "Tampa": 220,
    "Los Angeles": 475
}

def plane_ride_cost(city):
    return city_prices[city]

EDIT:

I should also mention that with my example we can use variables so that we could create a simple way of adjusting the prices of our cities dynamically.

EXAMPLE:

charlotte = 183
pittsburgh = 222
tampa = 220
los_angeles = 475
city_prices = {
    "Charlotte": charlotte ,
    "Pittsburgh": pittsburgh ,
    "Tampa": tampa ,
    "Los Angeles": los_angeles
}

def plane_ride_cost(city):
    return city_prices[city]

We can also create functions now to change our prices, we can throw all our variables into a list/dict/array and then work on them like that. There is tons of different ways we could do this that is better than the junk they showed us.

At your current skill level though using my first example would probably be the best way to learn this type of finding a value based on input.

Good luck!


#3

Thank you very much for your help!
:smile: