Final Code not printing Cost


#1

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

Code runs, but does not print total cost. What’s wrong?

def hotel_cost(nights):
    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)
    else:
        return("N/A")

def rental_car_cost(days):
    total = days * 40
    if days >= 7:
        total = total - 50
    elif days >= 3:
        total = total - 20
    return(total)

def trip_cost(city, days, spending_money):
    car = rental_car_cost(days)
    hotel = hotel_cost(days)
    plane = plane_ride_cost(city)
    total =  car + plane + hotel + spending_money     
    return(total)
    print(total)

trip_cost("Los Angeles", 5, 600)


#2

Hi @megamaster17697,

To print the cost, modify your final statement, as follows …

print trip_cost("Los Angeles", 5, 600)

This statement within the trip_cost function never executes, because the return statement preceding it terminates execution of the function …

        print(total)

It would be best to remove the print statement from the function. You could move it to before the return statement. However, it is better to use an external print statement to display the value that is returned by the function. It affords the programmer more flexibility regarding how to use that value.


#3

Thanks, that worked. I didn’t realise that it terminated the function.


#4

When a return statement executes, it terminates execution of the function that contains it, as it returns a value.


#5

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