FAQ: Introduction to Functions - Review

When I keep (STR) in the print statements but remove “Car” from the function call I get this message at the bottom.

Try using an argument such as ‘ship’ instead of ‘Car’.

Try removing the last comma from the function call (in your screenshot). While the syntax is correct, perhaps the automated grader has not been configured to accept it.

destination_setup("Alaska", "New York", estimate, "Ship")
# or
destination_setup("Alaska", "New York", estimate)

There is my code :
def trip_planner_welcome(name):
print("Welcome to tripplanner v1.0 " + name)
def estimated_time_rounded(estimated_time):
rounded_time = round(estimated_time)
return rounded_time
estimate = estimated_time_rounded(3)
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”)
destination_setup(“origin”, “destination”, estimate, mode_of_transport)

Last line : I have a n error : "mode_of_transport is not defined. Why ?
If i write “Car”, it’s ok. Yet the value is defined in the function.

Indentation is important in Python. Posting code directly in the forums messes up the formatting and indentation which makes it harder to read and understand the code.

To preserve code formatting in forum posts, see: [How to] Format code in posts

This is a sketch of your code:

def destination_setup(origin, destination, estimated_time, mode_of_transport="Car"):

destination_setup("origin", "destination", estimate, mode_of_transport)

In the definition of the destination_setup function, there are four parameters. Parameters are placeholders and allow your function the abstraction and flexibility to handle different inputs (arguments) without the need of the function having to be re-written or modified. There is no compulsion that your arguments must have the same name as the parameters.

Arguments are the actual values that you provide to the function when making a function call.

When you make a function call, the first argument will be assigned to the first parameter, the second argument to the second parameter and so on.

Suppose we make the function call,

destination_setup("Boston", "New York", 3, "Plane")

then the first argument "Boston" will be assigned to the first parameter origin. Within the body of the function, we will use the parameter name origin to access the provided value.
The second argument will be assigned to the second parameter and so on.

In your function call,

destination_setup("origin", "destination", estimate, mode_of_transport)

estimate has a value because of your earlier statement estimate = estimated_time_rounded(3).

But, the global variable mode_of_transport has not been assigned a value anywhere in your code.

The mode_of_transport in the function definition is the parameter of the function and is local to that function.

Outside the function, you have not declared and assigned a value to any variable named mode_of_transport.

mode_of_transport = "Car" in the function definition means you have assigned a default value to the function’s parameter. If the user provides an argument during a function call, then it will override the default value. If the user omits the argument, then the default value will be used.

destination_setup("Boston", "New York", 3, "Plane") 
# "Plane" will be assigned to the mode_of_transport parameter.

destination_setup("Boston", "New York", 3)
# Since no argument has been provided for the fourth parameter, so
# default string value "Car" will be assigned to mode_of_transport parameter.  
# Values assigned to variable in global scope
estimate = estimated_time_rounded(3)
mode_of_transport = "Bicycle"

destination_setup("origin", "destination", estimate, mode_of_transport)
# This function call will work
# The argument "Bicycle" will be assigned to the mode_of_transport parameter 
# of the function and will override default value of "Car"


ok, all is clear,
thank you very much.

@m33pkittyfawk @smulhern03 and anyone else that’s having issues in this review exercise. I think step 4 is very case sensitive as in you need to output exactly the message in the example.


In @smulhern03 it’s missing “hours” in line 9.

In @m33pkittyfawk’s code for example, you capitalized “Hour” in line 16. That won’t get you the check mark.

Just noting it here in case anyone else is wondering the same.

@board6656560075 Welcome to the forums!

That’s not my experience with the exercises. This one at the very least is straight forward and you may need to reread the exercise to make sure you fully understand what it is asking for. If your answers are very different from the solution perhaps you have interpreted the exercise incorrectly.

Take the first step as an example:

First, like in our previous exercises, we want to make sure to welcome our users to the application.

Create a function called trip_planner_welcome() that takes one parameter called name. The function should use print() to output a message like this: Welcome to tripplanner v1.0

Where <Name Here> represents the parameter variable of name we defined.

Call trip_planner_welcome(), passing your name as an argument.

So right away, you are going to be making a function called trip_planner_welcome().

It has one parameter name.

The body is going to print out a message: Welcome to tripplanner v1.0

You will need to insert the parameter nane in the printed message.

Finally, call the function.

There’s not very many ways you can do this if you follow it one step at a time.

When in doubt, ask the forum!

@jsantorno consider it to be practice for reviewing everything you learned in the functions module? This specifically test your knowledge of built-in functions and user defined functions.

def trip_planner_welcome(name):
print("welcome to tripplanner v1.0 " + name)
it asks me if my function prints the correct message to the terminal, i coded it just as it wanted me to, but it is not working. is there any way i can bypass the… i don’t know what the thing is called that checks the output to see if you’ve done exactly what it wants, but yeah, bypass that so i can continue?

So, when you called the function trip_planner_welcome() what was the result in the terminal?

Also, it’s helpful if you could please format your code so it’s readable.
Use, the </> button above or here.


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

terminal =
welcome to tripplanner v1.0 theDispatcher

though i just realized, it may be angry at this the other function i made:

list-type LFSR(a psudo-random number generator)

#set register to starting seed for rng(default is [0, 0, 0, 0, 0, 0, 0, 1])
#set taps for where in register the bits are xor’d(default = [0, 1])
#set iterations for the amount of times it shifts plus one(default = 1)(runs twice on default)
#set period to the, well, period that you want to output a number, 1 is default
#set output type to “binary” to dispaly raw numbers from register, “decimal” to display decimal equivalent

def LFSR(register = [0, 0, 0, 0, 0, 0, 0, 1], taps = [0, 1], iterations = 1, period = 1, output_type = "decimal"):
  shiftIn = 0
  cycle = 0
  def shift():
    i = 0
    while i < (len(register) - 1):
      register[i] = register[i + 1]
      i += 1
    register[len(register) - 1] = shiftIn
  def get_taps():
    i = 1
    accu = register[taps[0]]
    while i < len(taps):
      accu -= register[taps[i]]
      if accu != 0:
        accu = accu / accu
      accu = round(accu)
      i += 1
    return accu
  def printOutput():
    if cycle % period == 0:
      if output_type == "decimal":
        output = register[0]
        output_length = 1
        while output_length < len(register):
          output += register[output_length] * (2 ** output_length)
          output_length += 1
      if output_type == "binary":
        output = str(register[len(register) - 1])
        output_length = 1
        while output_length < len(register):
          output = output + str(register[(len(register) - 1) - output_length])
          output_length += 1
  while cycle < iterations + 1:
    shiftIn = get_taps()
    cycle += 1

after testing though, same problem
is this the format you were asking for?

Yea, if you add other non-lesson related items you will likely get an error. Comment that other function out.

it’s gone, but i still get the error, does it just need to refresh?

resetting doesn’t work

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

still ‘Does your function print the correct message to the terminal?’
i’m going to take a break, maybe it’ll work itself out in my head later

Even tho there is output in the terminal, I think they want you to use the print() to call the function.

I can’t view the instructions. Make sure that the string you print matches the instructions exactly (punctuation/spelling/spacing etc.)

You are printing "welcome ...", whereas other posters in the thread have "Welcome ..."

Do the instructions specify the message to be printed? If they do, make sure your output matches the specifications exactly.

This is the message they want you to output:
“Welcome to tripplanner v1.0 (Name Here)”

The instructions say, ’ Call trip_planner_welcome() , passing your name as an argument.’

But, often in lessons they want you to use print(), even if they don’t expressly say so. (which is the opposite of what one would do in Colab or Jupyter to execute code).