There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Hi, I noticed something on this exercise, it’s just a little thing. Not a criticism!
There are no indentations in the blue and yellow function in your descriptive text before the two lines that start with print in the function. That might be a small error(?)
it looks like this
def trip_welcome(destination):
print(" Welcome to Tripcademy! “)
print(” Looks like you’re going to the " + destination + " today. ")
So I understand the successful implementation of parameters and arguments thus far,
def generate_trip_instructions(location):
print("Looks like you are planning a trip to visit " + location)
print("You can use the public subway system to get to " + location)
generate_trip_instructions(“Central Park”)
Everything here works as it should, but my question is more toward the logic of it. Why does it work? If Python is a top down read. How is it that we can call a function, and print out (“string” + parameter), but the parameter is defined after we call the function?
Similar to if we were to print a variable, but define the variable after we try to print, it would error out and say that the variable isn’t defined. Just throws me for a loop a bit. I was really caught up on this one for awhile, as I kept trying to define the parameter before the function and it continually errored.
It might help to think of calling the function as a way of creating a second mini program. It has its own names and instruction set but it’s empty. It’s only when you call it and pass arguments to it that it actually gets loaded to memory that those names are assigned to actual data/objects. What’s more you can call it more than once with entirely different values and the names are just assigned to these new values and the same instructions are executed.
The function call effectively acts like a temporary assignment to that parameter function(parameter=argument). It is used in the mini program, it returns a value and then it is simply forgotten once that function has finished executing. So the names/variables in your new mini program stay the same but what they refer to changes on each new call.
def func(x):
return x + 4
# The new function has a parameter x
# This parameter is effectively empty
# Only when we call it does something get assigned
print(func(2))
# Once the value is returned what `x` refers to is forgotten
print(func(5))
# If it helps you can use the following syntax for this example...
print(func(x=10))
I figured it out the next day, after a solid night sleep and some brain debugging… It was kind of a stupid question once it dawned on me. I sometimes get into the thick of it, and kind of forget the basics. “See the forest through the trees” kinda scenario.
It makes perfect sense now. As soon as we use def __ folllowed by the indents, its all self contained and nothing is being ran. Once we call the function with its argument, that argument is applied to the function, and the function is ran beneath the calling of it… thus its still operating top down…
My brain just saw, “hey, that stuff is above it! How is it working like that?”
Anyway, I appreciate the in-depth response as it helps me reinforce my understanding of it thus far!
You wrote the argument as "Cenetral Park". It should be "Central Park".
In your function call, you spelled the function as generate_trip_instruction. It should be generate_trip_instructions.
The function call needs to be outside the function definition. You have the function call at the same indentation as the body of the function, which means the function call is interpreted as being part of the body of the function. Instead, the function call shouldn’t be indented.
def generate_trip_instructions(location):
print(...
print(...
generate_trip_instructions("Central Park")
# No indentation
Unlike some other languages, indentation is important in Python (Some languages use curly braces etc. instead of indentation to indicate the body of a function/method).
In Python, the statements meant to be the body of the function must be indented. The function call is not part of the body of the function and should therefore not be indented.
def generate_trip_instructions(location):
print("Looks like you are planning a trip to visit " + location)
print("You can use the public subway system to get to " + location)
generate_trip_instructions("Central Park")
The print statements are meant to be in the body of the function and are therefore indented.
The function call is not part of the function and is not indented.
Hi @cloud6659013659 , I had the same error initially and it’s because without the space, your location is going to go right against the rest of your print statement and therefore not the intended output.
Check the difference below:
def generate_trip_instructions(location):
"""
Generates trip intructions based on user-inputted location
"""
print("Looks like you are planning a trip to visit " + location)
print("You can use the public subway system to get to " + location)
def generate_trip_instructions_with_no_space(location):
"""
Generates trip intructions based on user-inputted location (this one has no space)
"""
print("Looks like you are planning a trip to visit" + location)
print("You can use the public subway system to get to" + location)
generate_trip_instructions("Central Park")
generate_trip_instructions_with_no_space("Central Park")
@java3142839019 you can set other variable types like integer, float, boolean, etc to your parameters as long as you have coded the function body to accomodate the intended variable type the arguments will be.
In this example, depending on how you coded the body of your print text, you could have entered integers, but may throw an error if you concatenated the parameter variable in your print rather than using the comma method.
Loving the course but this one, section Parameters & Arguments section 6, part 2 was incredibly confusing and frustrating all because of what looks like one space.
I had
Your code below:
def generate_trip_instructions(location):
print(“Looks like you are planning a trip to visit”+ location)
Which didnt work, I could for life of me realise why, then I added one space after the “t” in visit and it is green, see below.
Your code below:
def generate_trip_instructions(location):
print("Looks like you are planning a trip to visit "+ location)
I get you probably have a test running in the background but my god can you not add the potential answer without space or make the hint a little clear to use the space?
Hi, other than parsing through the function definition, is there a way to see how many parameters and what type are expected when calling the function?