FAQ: Introduction to Functions - Multiple Parameters

This is a good idea. I don’t think there was any intentional reason as to not include a coupon variable, I think they just were aiming to use less variables. Perhaps if there was no value for coupon (i.e. paying full price), then the function would err.

Either way, this is good thinking.

Edit: Yes, I just tested it with out coupon being defined. Gives a NameError. I would say that they did this so that you don’t have to enter a value for coupon each time.

Correct: hotel_total = hotel_rate * trip_time - 10

But this should also be correct, even though it says I got it wrong: hotel_total = (hotel_rate * trip_time) - 10

Is this code incorrect for what is being asked? Does it not reach the same conclusion that is being asked for in the lesson?

As far as Codecademy is concerned, I’m not adding the variables that are clearly being added together in the variable total_expenses.

Please advise me on how I can keep my code as is, and functional, and get past this lesson. I would rather not change the code just to fit the diff either. If this code works, I would like for it to be parsed correctly and be accepted as an answer by Codecademy’s system.

Thanks!

# Write your code below: 
def calculate_expenses(plane_ticket_price, car_rental_rate, hotel_rate, trip_time):
  #
  car_rental_total = car_rental_rate * trip_time
  hotel_total = hotel_rate * trip_time - 10
  total_expenses = plane_ticket_price + car_rental_total + hotel_total

  print("Your total sum for all your vacation's expenses comes to $"+str(total_expenses))
  #
calculate_expenses(200,100,100,5)

Step 4 specifies,

Lastly, let’s print a nice message for our users to see the total. Use print to output the sum of car_rental_total, hotel_total and plane_ticket_price.

Consider making the change:

# You wrote:
print("Your total sum for all your vacation's expenses comes to $"+str(total_expenses))

# Change it to:
print(total_expenses)

There is nothing wrong with your code, but I suspect the automated grader/checker is expecting just the total to be printed (as opposed to a string). I know Step 4 wants us to print a nice message, and your printed string is certainly nicer. But, the system is probably looking for just the total without any other text. A human grader would accept your solution, but the automated system expects a very specific output so that it can check whether it matches the expected output.

Is it possible to track units in the code, i.e., “days” or currency, or is that only possible in the comments?

@vvhitvvorth @board7281515882 Hi! Welcome to the community!
it is always much more helpful if you can post the code for our reference if you want some debugging advice. Without posting your code it’s impossible to determine if it’s something else that went wrong. Is is indented properly? Are the calculations prior performed correctly? Always post your full code when asking questions because there are many way to approach the same problem and context really helps.

@board7281515882 Halimah, for your other observation the order of operation in this particular instance would have taken care of it - multiplication and division always come first but there’s nothing wrong with using parentheses to help you along if you can’t remember. You do raise a very good point in the example there.

@slaughterwater @remco1123 You guys are not wrong. I think it’s just to keep the lesson simple while introduce the idea of multiple parameters. I think coupon could absolutely be added as a parameter and an additional input for the function to be even more dynamic. In your code @remco1123 though, the coupon value assignment needed to be inside the function for it to be working properly.

@henriksingh it’s because the default setting in the print function when you use comma to separate the arguments, it adds an extra space. You can modify the default sep by having an additional argument in the print statement like this:

car_rental_total = 500 hotel_total = 490 plane_ticket_price = 200 print("Your total is: $", car_rental_total + hotel_total + plane_ticket_price, sep="")

Continuing the discussion from FAQ: Introduction to Functions - Multiple Parameters:

@bellatrix55 Hi! Welcome to the community.

I don’t believe so. Most databases and programming languages for a given variable only tracks the type of variable (ie. string, boolean, float) and requires either a secondary variable or the user inputted programming to track the units elsewhere. This is likely because the units are really strings for our purpose. As we learned previously, strings and numeric values don’t mix very well and concatenation and calculations can really only be done on the same types of variables.

For the most part, the unit is only relevant for the user outputs for display and clarification purposes. You can always code additional code or even write a function to ensure that when the given value is displayed to always represent it with certain units.

1 Like

how do you make certain parameters optional? i’ve seen examples of functions that have lots of parameters but don’t require you to fill them all out.
like drawing a box in javascript, it requires the x and y coordinates as usual, but it also has width and height parameters that, if left blank, default to a number and keeps going with the code, not throwing any errors, how to make a function in python that does something similar?

Someone could support me? My code is like this, but appear a message erros asking if i sum the “car_rental_total + hotel_total + plane_ticket_price” but clearly i sum.

def calculate_expenses(plane_ticket_price, car_rental_rate, hotel_rate, trip_time):
car_rental_total = car_rental_rate * trip_time
hotel_total = hotel_rate * trip_time - 10
expense = car_rental_total + hotel_total + plane_ticket_price
print(expense)

1 Like
  • At the bottom of the exercise, there should be a “Copy to Clipboard” button. Use it to copy all of your code.

  • To paste your code in the forums with proper formatting, use the </> button (see this for more details: [How to] Format code in posts)

  • Mention which step of the exercise is not being accepted.

1 Like

I am having the same issue

Hello, I don’t know if you are still having the same issue. But I just spent a long time having it too. Turns out, you have to use: print(car_rental_total + hotel_total + plane_ticket_price) and then that worked for me.

I had every other line the same as yours

I am not sure what I am missing here in my code:

def calculate_expenses(plane_ticket_price, car_rental_rate, hotel_rate, trip_time):
car_rental_total = car_rental_rate * trip_time
hotel_total = hotel_rate * trip_time - 10
trip_total = car_rental_total + hotel_total + plane_ticket_price

return trip_total

Step 5: call your function

print(calculate_expenses(plane_ticket_price=200, car_rental_rate=100, hotel_rate=100, trip_time=5))

I am receiving the error message " Did you call the function calculate_expenses with the proper argument values? Make sure to call them in the correct order!"

If anyone could help that’d be great.

Assignments can only be used in the parameter list to set default values. Just pass in the numbers, not the variable assignments.

print (calculate_expenses(200, 100, 100, 5))
1 Like

Thank you so much, thats such a simple fact that I overlooked I can’t believe it was that simple!

1 Like


how long does it take to fix a bug when its reported?

1 Like

Heed the error message. Did you delete return trip_total?

2 Likes

I did and now its resolved, I clicked the “replace with solution” option earlier which was exactly the code i put and kept getting errors. I took a day off watching youtube videos on python but have no idea if the issue would be resolved for future users to have, “replace with solution” option with the “return trip_total” text added since it was not added in the “replace with solution” option. But thank god viewing this forum helped because I wanted to continue learning coding.

What path or course is this lesson? Comp. Sci? The OP had unrelated course links in it.