FAQ: Introduction to Functions - Review

Remember than any code that is part of a function needs to be indented under the function declaration like so:

def add_five(num):
  num += 5 # this is code that is indented
1 Like

Stuck on why I’m getting a syntax error on step 3. i’ve checked some of the other code and I seem to be typing the exact same code but receive an error and can’t determine where it is incorrect. Thanks in advance for any help.

def trip_planner_welcome(name):
  print ("Welcome to tripplanner v1.0 "+ name)

def destination_setup(origin, destination, estimated_time, mode_of_transport= "Car"):
  print("Your trip starts off in " + origin)
  print("And you are traveling to " + destination)
  print("You will be traveling by " + mode_of_transport
  print("It will take approximately " + str(estimated_time) + " hours")

For the last print stmt with estimated_time, I receive the following error: “File “travel.py”, line 10
print(“It will take approximately " + str(estimated_time) + " hours”)
^
SyntaxError: invalid syntax”

Syntax errors can be tricky sometimes, occasionally you’ll catch problems from preceding lines like unclosed parentheses. Have a close look at the preceding lines here to make sure they’re all OK.

You’re right, was missing the last parenthesis on the prior line. Thank you.

1 Like
trip_planner_welcome(" Naomi Shark ")
estimate = estimated_time_rounded(2.43)
destination_setup(" York ", "Durham  ", estimate, "car")

Hi there, does anyone know why this is printed into:
Welcome to tripplanner v1.0 Naomi Shark
Your trip starts off in York
And you are traveling to Durham
You will be traveling by car
It will take approximately 2 hours

Why in terms of output, the order is name → location starting off → destination etc, rather than what was written in the code? Many thanks! (this is the last exercise)

1 Like

Welcome to the forums!

It seems that the output is in the correct order, or at least in the order the exercise wants you to output it. Can you post your code?

I don’t understand why my estimated time doesnt round to 3
i think it must be something wrong in the code but i really can’t find the error. Can someone help me please?

def trip_planner_welcome(name): print("Welcome to tripplanner v1.0 " + name) trip_planner_welcome("Isabel") def destination_setup(origin = "Santiago", destination = "Maracaibo", estimated_time = 2.43, mode_of_transport = "Car"): print("Your trip starts off in " + origin) print("And you are traveling to " + destination) print("You will be traveling by " + mode_of_transport) print("It will take approximately " + str(estimated_time) + " hours") destination_setup() def estimated_time_rounded(estimated_time=2.5): rounded_time = round(int(estimated_time) + ((estimated_time - int(estimated_time)) /0.6)) return rounded_time print(rounded_time)

You have a function, estimated_time_rounded that I don’t think actually gets called, make sure you call it if you want it perform its task e.g. function(args). Be careful with the placement of statements in a function, if return executes before them then they’ll never be reached.

So why is my code not actually rounding anything? The time being printed is simply the string I input, it’s not actually rounding…see below:

# Write your code below: def trip_planner_welcome(name): print("Welcome to tripplanner v1.0 " + name) def destination_setup(origin, destination, estimated_time, mode_of_transport = "Car"): print("Your trip starts off in " + origin) print("And you are traveling to " + destination) print("You will be traveling by " + mode_of_transport) print("It will take approximately " + str(estimated_time) + " hours") def estimated_time_rounded(estimated_time): rounded_time = round(int(estimated_time) + ((estimated_time - int(estimated_time)) /0.6)) return rounded_time #Calling the functions above trip_planner_welcome("My Name") destination_setup("Get Set", "GOOOO!!!!", 0.5)

Welcome back to the forums!

Take a closer look at the line where you print out the estimated time. It looks like you’ve forgotten to call your function.

So, it kind of was a matter of not calling the estimated time function, but it has to be called as a parameter of destination_setup, which - maybe I’m just being stubborn - seems like it should be part of the lesson.

And for that matter, why is estimated_time_rounded function scoped outside of the destination_setup function. Wouldn’t it be easier to have it inside of destination_setup, so that one can simply specify the estimated_time parameter and not have to…type so much?

Not necessarily. It makes more sense to just call the function within the body of destination_setup.

By organizing different parts of our code according to their function, we are making our program more modular and easily modifiable. Ideally, we should have a different function for each thing our code does. Any function that has more than one purpose can be broken up into smaller functions. If we ever wanted to use estimated_time_rounded for another purpose outside of destination_setup, writing it as a separate function would allow us to use it. If it were inside destination_setup, it would be only accessible within the scope of destination_setup.

1 Like

It makes more sense to just call the function within the body of destination_setup

Hang on, maybe I’m missing something…it is early in the morning and I’m only on my second coffee so please be patient, but: the lesson didn’t have me call estimated_time_rounded within destination_setup. I initially thought this was going to be the approach of the lesson, but instead it had me call both functions, and one of them had to be done so as a parameter of the other…

In the end I did get the output that the lesson was looking for without showing any hints, but it took me going away form what I would think would be a common sense approach: if each function is scoped by itself, why aren’t we also calling that function from within another, rather than having to call it as a parameter…I think…

Looking at the solution on the site, it looks like what they did was create a globally-scoped variable to hold the value of calling estimated_time_rounded() on a number. Then they passed that variable as a parameter to destination_setup(). It doesn’t actually expect you to call estimated_time_rounded() within the call to destination_setup(), though you could.

There are many, many ways to write this program and some are more readable than others, some more concise, involve less variables, etc. Here are the three that we’ve discussed, which all work perfectly fine (this is by no means an exhaustive list):


Option 1: Calling Within The Function

def destination_setup(estimated_time):
  print(str(estimated_time_rounded(estimated_time)))

destination_setup(2.43)

Option 2: Calling In The Function Call

def destination_setup(estimated_time):
  print(estimated_time)

destination_setup(estimated_time_rounded(2.43))

Option 3: Storing In A Variable

def destination_setup(estimated_time):
  print(estimated_time)

rounded = estimated_time_rounded(2.43)
destination_setup(rounded)
2 Likes