Pull it together. I think there's a bug?


#1

For the exercise :
Pull it together

When I run the code it gives the following error:
Oops, try again. trip_cost('Pittsburgh', 1) returned 363 instead of the correct value 402

If I run it again, it cycles through the locations like so:
Oops, try again. trip_cost('Charlotte', 8) returned 363 instead of the correct value 1573
Oops, try again. trip_cost('Los Angeles', 2) returned 363 instead of the correct value 835
(etc etc)

Below is my code:

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
        
def rental_car_cost(days):
    if days >= 7:
        return 40*days-50
    elif days >= 3 and days <= 6:
        return 40*days-20
    elif days <=2:
        return 40*days
        
def trip_cost(city, days):
    days = 1
    return rental_car_cost(days) + plane_ride_cost('Charlotte') + hotel_cost(days)

Can anyone see anything glaring?

EDIT: I've used 'Charlotte' and "Charlotte" and get the same error. When I say it cycles through error city names at the start, that is with still using Charlotte. If I change to say "Tampa", it still cycles through various city name errors. It's so strange.

Thanks so much...


#2

inside your trip_cost function, you shouldn't set days to 1, this will overwrite the argument passed in the function when called. Also, inside the trip_cost function, when you call plane_ride_cost, you should pass city as arguments, not a fixed string city

If you need more help after these changes, post an updated version of your code


#4

Thank you, here is the code that I now have:

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
        
def rental_car_cost(days):
    if days >= 7:
        return 40*days-50
    elif days >= 3 and days <= 6:
        return 40*days-20
    elif days <=2:
        return 40*days
        
def trip_cost(city, days)
    return rental_car_cost(days) + plane_ride_cost(city) + hotel_cost(days)
    
trip_cost("Tampa", 1)

This is the error now:

  File "python", line 22
    def trip_cost(city, days)
                            ^
SyntaxError: invalid syntax

I'm starting to go blurry looking at this code. It must be something SO simple.... :frowning:


#5

after the function declaration there must be a colon:

def trip_cost(city, days): <- colon (:) is mising in your case

#6

embarassing :slight_smile: I think I'm too tired. I added the : and it worked.

Thank you again.

One question though. I thought that a variable declared inside a function was local to that function. So if I declared the days in that function, why would it carry over to the other functions and override them?

Thanks again...


#7

using this and that makes it really vague what you are referring to. You can pass parameters as arguments to other functions, that is no problem

the problem arises when you create a local variable inside a function and then try to access it outside the function:

def example():
   test = 10
example()
print test

but this is not what you do, you simple pass arguments into the function, that is completely different


#8

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