Taking a Vacation (Pull It Together) What's wrong with my code?


I have attached my code to this questions, can someone please explain why I am getting an error? Am I not doing the math correctly, i.e.. use of arithmetic operators or the syntax is incorrect. Why cant it calculate properly?


Remember to include hotel cost in your total trip cost.


The problem is that the exercise only wants two arguments and it doesn't matter what two arguments I choose. And if I call the hotel_cost function then a syntax error occurs. I have attached two screenshots explaining this conundrum.


Use the days parameter in the call. nights is local to the hotel_cost function.


I am already using days. What are you referring to? The first screenshot shows that clearly.


The first screenshot has no call to hotel_cost, as mentioned earlier, and the second one is calling hotel_cost with nights when it should be days.


Thank you for pointing that out, now for some explanation if you don't mind. How did you understand the code having the problem?
BTW I have marked your response as the answer to this thread, thanks again.


I generally start a trace from the call made to the function. What are the arguments, and especially what is their order? This is important because the function parameters are assigned in the same order. We don't want to mix our variables up else there could be computation errors or type mismatches. So that leads to the next step in the investigation, the function definition. What are the parameters?

Recall that often the arguments are literals, as in this case. We can see that the city is a string, and that the duration is a number. The function loads those values into the parameters in that order, so if the definition doesn't look like this,

def trip_cost(city, days):

then there is a problem. Now that we are inside the function, we are faced with a group of calls that we would then trace back to their respective functions, again paying close attention to the argument on the one hand, and the parameter on the other. By this process we eventually come to hotel_cost(nights), but looking over at the trip_cost parameters, there is no nights variable, only, days which means nights is undefined, hence the exception.

Note that it does not matter what the variable is called in hotel_cost, only that we use a variable that is present and defined in the calling scope.

Part of the back trace includes of course close examination of the function code so we can verify how the parameters are put to use. Follow the data as it courses through the program and see if you get the outcome you expect with various inputs.

With lots of practice, things will start to jump out at you on first glance, especially if you adopt recommended style guide approaches and keep your code organized and well labeled.


Thank you for the explanation.


I copied the above example as correctly as I can tell, yet it still doesn't accept the answer. I'm missing something simple I know it I just can't figure out what it is. Help???


The parameters for trip_cost are in the wrong order.


Thank you so much! That works perfectly. It's been fun learning Python but it's little things like that that I just need to learn more about.