My code runs fine in TextWrangler but the codecademy editor throws error message


#1

Hello,

I have run this code in TextWrangler and got correct result. But when I run it in the codecademy editor it throws different errors every time. It is quite frustrating. Because I know it is correct but I do not have any way of skipping this stage.
Please see below a link to my exercise.

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

Also please see below a copy of my code.

def plane_ride_cost(city):
    if city=="Charlotte":
        return 183 
    elif city=="Tampa":
        return 220
    elif city=="Pittsburgh":
        return 222
    else:
        return 475
        
def hotel_cost(nights):
    return 140*nights
        
def rental_car_cost(days):
    if days>=7:
        return 40*days-50
    elif 7>days>=3:
        return 40*days-20
    else:
        return days*40
    
def spending_money(days):
    return 200*days
    #expense
    
    
def trip_cost(city,days,nights):
    return plane_ride_cost(city)+rental_car_cost(days)+spending_money(days)+hotel_cost(nights)
print trip_cost("Tampa",3,3)

Following are the error messages
Oops, try again. trip_cost('Charlotte', 1, 0) returned 423 instead of the correct value 363
Oops, try again. trip_cost('Tampa', 7, 0) returned 1850 instead of the correct value 1430
Oops, try again. trip_cost('Los Angeles', 3, 0) returned 1175 instead of the correct value 995
Oops, try again. trip_cost('Tampa', 3, 0) returned 920 instead of the correct value 740

However at the output window I get no errors but the correct solution
1340
None

Or I am getting mad! I know there is something very simple or obvious I am missing out. Thanks in advance for you response.

Best wishes


#2

from the instructions:

Modify your trip_cost function definition. Add a third argument, spending_money

your third argument is nights, and spending_money is a fixed amount, don't multiply it with days.

this comparison is wrong:

elif 7>days>=3:

if you want to do multiply comparisons, use a and or or operator, or just don't check the days are less then 7, they are, if a if clause runs, elif can never run.

If you need more help, post an updated version of your code. Yes, the code runs, but if you do something different then the instructions, you get an error message. There is a limit to the freedom in the exercises

For the hotel_cost you should also use days, it is fine to improve/change a program, but it is not a good idea to do this in the lesson


#3

It works now! Many thanks for your help. One more question - how can I link raw_input function to print trip_cost("Tampa",3,300)? Or I should not go ahead of the current lesson.

Best wishes


#4

you can add raw_inputs and store the result in a variable and pass it as argument in the function call?

city = raw_input("Which city would you like to go to?")
trip_cost(city, 3, 300)

careful, this doesn't validate the city (if the city is actually in the lists of destiny's). And if you want to prompt for nights and spending money, don't forget to cast them to integers (raw_input stores as unicode/string), you then of course would need to validate that the cast can be done (if you want to do it right), if the user enters a string, and you don't validate, the program crashes


#5

Thanks for your advice. It works now. Perhaps I will learn how to limit the search to the preset list of cities later.

Best wishes,


#6