0

look at the error message ,everything i can't find the error ,we insert the value into function trip_cost('Los Angeles', 4) , and city will be string and days will be int type ,is that any knowledge i should know?

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
    else :
        return 123

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

        total=total-20
        return total
    else :
        return total
def prev_add(a,b):
    return sum(a,b)
def trip_cost(city,days):
    nights = days
    hotel_cost_price = hotel_cost(nights)
    rental_car_price=rental_car_cost(days)
    prev_price = prev_add(rental_car_price,hotel_cost_price)
    plane_ride_price = plane_ride_cost(city)
    return sum(prev_price,plane_ride_price)

========================

error code :

 trip_cost('Los Angeles', 4) raised an error: 'int' object is not iterable 

===================

1
  • Out of curiosity, what are you writing/running this in? I remember one of my students giving me an error like yours and I was really confused because there wasn't a traceback showing where the error was at all levels (not just the top-most, e.g. trip_cost call). I really hate the "friendly" interfaces that hide crucial detail. Commented Dec 25, 2014 at 9:45

3 Answers 3

4

sum() computes the sum of an iterable, such as a list. However, you are trying to use it to add two numbers:

def prev_add(a,b):
    return sum(a,b)

If you want to simply add a and b, use +:

def prev_add(a,b):
    return a + b

In this case, I would get rid of the function altogether and just use addition.

The same goes for your use of sum() in

return sum(prev_price,plane_ride_price)
Sign up to request clarification or add additional context in comments.

3 Comments

would you also like to suggest def prev_add(*nunbers): return sum(numbers),
@GrijeshChauhan: Interesting suggestion, thanks. However, I feel it's much too advanced for this question (and in this case doesn't really add anything over the simple use of +).
you are correct, few days back I started reading cookbook (an advance course) where I found this suggestion from there
2

Just check the sum help documentation. sum(3,2) will fail, but sum((3,2)) will work. Since sum(3,2) is considered as sum function called with two parameters passed, while sum((3,2)) is considered as one parameter passed to the sum function.

>>> help(sum)
Help on built-in function sum in module __builtin__:

sum(...)
    sum(sequence[, start]) -> value

    Return the sum of a sequence of numbers (NOT strings) plus the value
    of parameter 'start' (which defaults to 0).  When the sequence is
    empty, return start.

>>>

Comments

0

It's simpler, you don't need the extra prev_add function.

The solution is to set your city variable (which you pass to trip_cost) to the result of plane_ride_cost(city) inside trip_cost

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.