# FAQ: Introduction to Functions - Multiple Parameters

``````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
print(car_rental_total + hotel_total + plane_ticket_price)
calculate_expenses(200, 100, 100, 5)
``````

Hi, everything in this code is fine, but I just wanna ask why we do not need to add str( ) for the parameters in the print statement. Is it because we only use str( ) for printing variables that have numbers assigned to them, and we dont need to use str( ) for parameters?

There’s a couple of things on the go here so I’ll try and break it down. You may have seen simple `print` statements like `print(3)` before. Part of the machinery of `print` will try and get a string representation of this integer object so that `print(str(3))` would have the same output as `print(3)`.

So no, you don’t have to convert everything to a string before using `print` because it will do some of the formatting for you.

The other part of this is that expression using `+` operators is evaluated before being passed to `print`. So your call acts much the same as the following would-

``````temp = car_rental_total + hotel_total + plane_ticket_price
print(temp)
``````

We know this is fine because we’re adding together three integers so we just get the sum of those integers as a new integer (and print does the formatting of that new integer for us).

You may have seen similar things that looked more like `print('some string' + str(number))`. If you consider what happens before the argument is ever passed to `print` then this makes sense.

``````# both of these will fail for slightly different reasons
text = '3' + 3
number = 3 + '3'
``````

We cannot concatenate a string with an integer like this, or vice versa (we’d get a type error). So we may convert the integer to a string `str(3)` and then join it to the other string in which case we pass a single string to `print` and everything makes sense again.

``````temp = '3' + str(3)
print(temp)
Out: 33  # this is a string after all, it's just two characters
``````
2 Likes

Hi guys,

I don’t get it. This is 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
print(car_rental_total + hotel_total + plane_ticket_price)

calculate_expenses(200, 100, 100, 5)

In Question 5 it says that the result is 1190. But I get a syntax error…WHY? What is wrong?

Hi!

Just wondering if someone can let me know if this is an acceptable way to pass this lesson, any other ways it can be improved would be appreciated as it would help my learning.

``````# 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
trip_total = car_rental_total + hotel_total + plane_ticket_price
print("Hello! The cost of car rental for the trip is " + str(car_rental_total) + ". The hotel rate will be " + str(hotel_total) + ". Finally, your ticket price is " + str(plane_ticket_price) + ".")
print("The total cost of your trip would be " + str(trip_total))

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

Hello,

Hopefully you managed to pass this lesson by now, but I thought I would reply to help you and anyone else who maybe comes across this issue.

I tested your code and the Syntax Error is on the first line where you are defining your function. You must end the function parameters with a colon. I believe this will solve the issue.

In the example code below, I also created a variable trip_total to hold the value of the calculation in your print statement.

``````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
print(trip_total)

calculate_expenses(200, 100, 100, 5)
``````

Hope this helps!

Evening,

I’m starting to find it a little difficult so thought I’d post my progress. The bot reporting if you’re right or wrong has thrown me a couple of times.

hotel_total=(hotel_rate * trip_time)-10

I wrote this calculation with parenthesis and I was told it was incorrect. It still gives the same answer without parenthesis.

In Part 4 you’re instructed to:

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` .

However if you include any text before the string it tells you that you’ve made an error.

print("Your total is " + str(car_rental_total + hotel_total + plane_ticket_price))

I might be wrong but that was my experience. I’ve passed the lesson now and I feel like I learned some things.

Thanks.

Can anyone help me troubleshoot this error?

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
print(car_rental_total + hotel_total + plane_ticket_price)
calculate_expenses(200, 100, 100, 5)

Error:
Traceback (most recent call last):
File “travel.py”, line 6, in
print(car_rental_total + hotel_total + plane_ticket_price)
NameError: name ‘car_rental_total’ is not defined

Please view How do I format code in my posts? as without it code loses formatting and more importantly indentation.

Make sure any statements that are part of your function are indented to the same level as the function, especially if they use function local names that are forgotten once the function has finished executing.

For example the following perfectly valid-

``````def func():
x = 3
print(x + 2)
return x

print(func())
Out: 3
``````

Whereas this will fail because there is no x in the outer scope (the `x` is local to the function, the function itself is denoted by its indentation and `print` is back in the outer scope).

``````def func():
x = 3

print(x)  # ERROR
``````
1 Like

I have a problem with the way the coupon is used.
Wouldn’t you want to edit the fuction as little as possible?
If the coupon price would change the fuction will always lower the price by 10 no matter what the coupon says.
If you make a new variable called “coupon” doesn’t that make it easier to change the coupon price?

# 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 - coupon print(car_rental_total + hotel_total + plane_ticket_price) coupon = 10 calculate_expenses(200, 100, 100, 5)
1 Like

In exercise 5 in this lesson, when I type the print statement in the following

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 print("Your total is: \$", car_rental_total + hotel_total + plane_ticket_price) calculate_expenses(200, 100, 100, 5)

it prints a space between the \$ and the total. How do I remove this space, as there is no space in the string “Your total is: \$” This is not really neccesary, but I was just wondering how to do it.

1 Like

Yes do not forget about the : colon at the end of ever def function you do. It’s very important.

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

#
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:

# 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?