Put it together


#1

Can someone tell me why my code isn't printing? It says that I haven't included the trip cost function but to my best understanding I have... (i'd be grateful for anyone who could explain it to me - have been trying different things without any luck for hours!)

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 'please enter a valid destination'
        
def rental_car_cost(days):

    if days >= 7:
        return (days * 40) - 50
    

    elif days >= 3 and days < 7:
        return (days * 40) - 20

    else:
        return days * 40

def trip_cost(city, days, spending_money):

    return plane_ride_cost('Los Angeles') + rental_car_cost(days) + hotel_cost(nights) + (spending_money * 1)   

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

#2

Hi, @raphaelreuben ,

Do you see any error messages in the console where output should be displayed, in addition to the message from Codecademy regarding the trip_cost function?

Your code has several problems that the Python interpreter may reveal one at a time as you correct each of them. Those error messages can be of great help for debugging a program.


#3

Hi @appylpye,

Thank you for responding. I have now got to a stage where the only error message is "It looks like nothing printed in your console" from codecademy, and 'None' printed where output should be.

I'm not quite sure how to proceed. If you can point out where i'm going wrong, it'd be really helpful - Thank you

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 'please enter a valid destination'
        
def rental_car_cost(days):

    if days >= 7:
        return (days * 40) - 50
    

    elif days >= 3 and days < 7:
        return (days * 40) - 20

    else:
        return days * 40

def trip_cost(city, days, spending_money):

    return plane_ride_cost('Los Angeles') + rental_car_cost(days) + hotel_cost(days) + (spending_money * 1)   

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

#4

actually, it looks like this

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 'please enter a valid destination'

def rental_car_cost(days):

if days >= 7:
    return (days * 40) - 50


elif days >= 3 and days < 7:
    return (days * 40) - 20

else:
    return days * 40

return rental_car_cost(4)

def trip_cost(city, days, spending_money):
return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money

return trip_cost('Los Angeles',5,600)

#5

Can you include the link to the lesson so i can look directly at the instructions and my code? i looked at Pull it Together, but it doesn't require the console to display anything so I would like the link to make sure I'm looking at the right thing.


#6

Link to exercise: Pull it Together.

The following statement is unnecessary and should be removed. It appears to be included in the rental_car_cost function, and would cause a problem with an infinite recursion, wherein the function calls itself repeatedly until the system puts a halt to it ...

return rental_car_cost(4)

This final line should be a print statement rather than a return statement, and should reside outside of any function ...

return trip_cost('Los Angeles',5,600)

Therefore you should replace return with print, and make sure that the line is not indented.


#7

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