Taking a vacation 5/7, weird reason for error


#1



I was currently doing the exercise Taking a vacation 5/7, in my opinion it is not very difficult and I thought Idid not make a mistake. After I pushed the save and submit button I received an error. After the former tasks (1/7 - 4/7) were all right, I assumed that I did a mistake during the step where you add the cost. After I tried a bit i just randomly switched the function a bit up, wich worked. But I cannot explain to me why it did not work before: Here ist the code and thank you for your help


def hotel_cost(nights):
    return 140*nights 
    

def plane_ride_cost(city):
    if city =="Charlotte":
        return 183
    if city =="Tampa":
        return 220
    if city =="Pittsburgh":
        return 222
    if city =="Los Angeles":
        return 475
        
def rental_car_cost(days):
    cost = days*40
    if days >= 7: 
        return cost-50
    elif days >=3 and days <= 7:
        return cost-20
    else:
        return cost
    
def trip_cost (days, city):
    return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city)
    
Error: trip_cost('Charlotte', 5) raised an error: unsupported operand type(s) for -: 'str' and 'int'

*******************************************************************************
#Thats how it works (I can't explain why, I only changed the order of city and days)
def trip_cost (city, days):
    return hotel_cost(days) + rental_car_cost(days) +  plane_ride_cost(city)


#2

here:

def trip_cost (days, city):
    return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city)
    
Error: trip_cost('Charlotte', 5)

days now contains Charlotte as value, so then we get:

hotel_cost('Charlotte')

well, hotel_cost can't really handle that input. Same for rental_car_cost:

rental_car_cost('Charlotte')

and same for plane_ride_cost, city now contains 5, which is not a valid destination


#3

I am new to Python, so can you tell me why this happens. Maybe it was once explained and I have already forgotten it. Why is there a difference depending on the order of the two parameters.


#4

we have two things, our function with our parameters:

# function with parameters days and city
def trip_cost (days, city):
    print days
    print city

# function call with arguments Charlotte and 5
trip_cost('Charlotte', 5)

the order of the parameters determine which argument belongs to which parameter

the first arguments belongs to the first parameter and the second argument belongs to the second parameter

go run this code to see days contains Charlotte and city contains 5

now realize what this does for your rental_car_cost, plane_ride_cost and hotel_cost function calls


#5

Got it thank you very much


#6

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