Incorrect math/logic in "Pull it Together"


#1



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


The code's math is incorrect. This is the passing code.

The final line should be:

return plane_ride_cost(city) + hotel_cost(days - 1) + rental_car_cost(days)

This is because you aren't paying for the last night during the trip.


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 "nope"

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

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


#2

And what about handling an invalid city input?

There are so many things which can be improved on this program, codecademy gives you the basic, you can then improve up on the program


#3

No, there is error handling in the program for dealing with bad city inputs.

Clearly this isn't the most advanced program, I will grant that. But the lesson should at least encourage critical thinking and use appropriate logic. You don't even have to force users to think of it themselves, just add it in the instructions.


#4

critical thinking is always highly encouraged, i would also encourage to expand the program


#5

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