Help~ "Pull it Together"


#1



Hi everyone,
I am troubling with the following,
it said that raised an error: sum expected at most 2 arguments, got 3

I would like to know how to fix it > <

Thanks a lot!


raised an error: sum expected at most 2 arguments, got 3


"""1st component"""
def hotel_cost(nights):
    return 140 * nights

"""2ed component"""
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

"""3rd component"""
def rental_car_cost(days):
    cost = 40 * days
    
    if days >= 7:
        cost -= 50
    
    elif days >= 3:
        cost -= 20
    
    return cost

"""combination"""
def trip_cost(city,days):
    return sum(plane_ride_cost(city),rental_car_cost(days),hotel_cost(days))
    


#2

sum() is a built in function to get the sum of an iterable, like a list

in your case, the function calls result in integers, which you can just add together like you would add normal integers together (+)


#3

This part of code which is creating a bug/erorr here ,as @stetim94 Already mentioned and even gave the reason about why it's not working!

"""combination"""
def trip_cost(city,days):
    return sum(plane_ride_cost(city),rental_car_cost(days),hotel_cost(days))

Reason:
Wrong use of sum() method.
How sum() works?

here is a small read:
sum()

Lets check how it works on lists.
sum() can maximum take two arguments!

lets see an example...

x = [1,2,4,5]
x_sum = sum(x)
print x_sum    // it returns 12

so sum works on list containing numbers.

How to make your code work ?
return sum(plane_ride_cost(city),rental_car_cost(days),hotel_cost(days))

You have to use sum() so all of the items should be inside the list..
like this
final_sum = [plane_ride_cost(city),rental_car_cost(days),hotel_cost(days)]

Then pass it to sum.
sum(final_sum)

I hope you figure out last part!

Update***
It is not a best way to do it , sticking to simple way will be more effective! :slight_smile:
further Update**
The approach mentioned by @stetim94 is way better than mentioned by me.
I strongly recommend using this.


I hope hint helps ya! :slight_smile:


#4

why use sum()? i would advise against it. Given the functions return integers, adding them together with + is a lot easier


#5

I was walking through coder's (@kevin015015) mindset of solving this problem - using sum() function
and Yep! :slight_smile: , I did mentioned it!

Update***
It is not a best way to do it , sticking to simple way will be more effective!

:slight_smile:


#6

sometimes, a different approach then the learners is recommended. Of course you can do:

return sum([plane_ride_cost(city),rental_car_cost(days),hotel_cost(days)])

but its not really great, it doesn't make much sense to solve this problem this way. It means first: python has to construct the list into memory (RAM), then sum has to loop over all the items in list and add them together.

using + is far more effective.


#7

Exactly and I agree!I :thumbsup:
As I said..

I was walking through coder's (@kevin015015) mindset of solving this problem.

update**
I've updated my post! :slight_smile:
thank you Mod :thumbsup:


#8

Yeah I tried using + in my code ( and it should work)... I get no error, but Codecademy keeps telling me:
Oops, try again. plane_ride_cost('Charlotte') raised an error: maximum recursion depth exceeded

And the thing is, there's no plane_ride_cost('Charlotte') in my code.
On the console it says:
None

I hope its okay to comment in this, :confused: but anyways, you think I did something wrong? If you need the code I wrote, let me know and I'll reply.


#9

can we see an updated version of your code?


#10

Sure!

def hotel_cost(nights):
    return 140 * nights
    
def plane_ride_cost(city):
    return 183 * plane_ride_cost(city)
def rental_car_cost(days):
    rental_car_cost = 40 * days
    if days >= 7:
        return rental_car_cost - 50
    elif days >= 3:
        return rental_car_cost - 20

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

There may be something wrong with my code, but I'm really confused by the error Codecademy gave me. :frowning:
In 3. Getting There, I don't know if I was supposed to put in:

"Charlotte": 183
"Tampa": 220
"Pittsburgh": 222
"Los Angeles": 475

so I didn't do that and they let me proceed which made me think that adding that must of not been necessary. I know that the Python courses are finished being under construction, so perhaps they were fixing any glitches, but when I came on to test my code again, I get the same error - yet I get no error on the console. :computer:
I can take a screenshot of what I'm seeing if this isn't enough... just let me know.


#11

well, this explains the error:

def plane_ride_cost(city):
    return 183 * plane_ride_cost(city)

its a function calling itself, this is known as recursion. Given you have no base case, its sort of like an infinity loop

Maybe revisit the exercise where you build this function?


#12

Ok, I'll revisit it then.


#13

So I don't need to add:

"Charlotte": 183
"Tampa": 220
"Pittsburgh": 222
"Los Angeles": 475

Is it fine without it?
If it is ok to not include that, I can rewrite the whole thing and get it to work again...


#14

you do need to add? its in the exercise where you build the plane_ride_cost function


#15

I'm looking at it right now...


#17

Or I can just do...?

def plane_ride_cost(city):
   if city == "Charlotte":
      return 183

#18

no, this is recursion again.

much better :slight_smile:


#19

Ah yes, I understand. Thanks so much! :smiley:


#21

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