FAQ: Introduction to Functions - Review

// Your code:
destination_setup(“Scotland”, “Spain”, str( 3.5) , “Car”)

// It should be:
destination_setup(“Scotland”, “Spain”, estimate , “Car”)

// The third argument of the destination_setup function should be a number. 
// You are instead passing in a string i.e. your third argument is str(3.5)
// Earlier in the code, you calculated integer value of time and saved it in a variable,
estimate = estimated_time_rounded(3.5)
// You can pass estimate as the third argument.
// The automated grader probably checks if the third argument is a number.

1 Like

In step 2 the instructions are to:

  1. Create a variable called rounded_time that is the result of calling the round() built-in function on the parameter estimated_time.
  2. Return rounded_time.

I get the same result by simply calling the “round()” function on the parameter “estimated_time” and returning that vs. defining it as “rounded_time” first. Is there a specific reason why it is defined first as rounded_time before returned?

The exercise doesn’t go from step 1 to the next step even if you do everything exactly the way it asks you to do. It replies that you have done something wrong. When you open up the solution page, you see that the correct answer different than what the exercise is asking you to do. Example it asks you to output “” and rejects it. But the solution says it should be “”. And these things are spread across the exercise. So you can’t solve it on your own since you don’t know what value it is looking for

Having some trouble on Step 4. As far as I can tell, my output is exactly what the exercise is asking for. Would appreciate any help

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

trip_planner_welcome(“Sean”)

def estimated_time_rounded(estimated_time):
rounded_time = round(estimated_time,0)
return rounded_time

estimate = estimated_time_rounded(2.43)

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(estimate) + " hours”)

destination_setup(“SFO”,“LA”,3.5,“Plane”)

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

# You wrote:
print("It will take approximately " + str(estimate) + " hours")

# It should be:
print("It will take approximately " + str(estimated_time) + " hours")

The relevant parameter of the destination_setup function is estimated_time (and not estimate).

When you make the function call,

destination_setup("SFO","LA",3.5,"Plane")

3.5 will be assigned to the estimated_time parameter. But you will see that one of the print statements gives the output:

“It will take approximately 2.0 hours”

which doesn’t match the argument provided by you during the function call.

I have a question. Why will some numbers round-up at .5 but some not? I’ve learned, at .5 the number must be rounded up.

def trip_planner_welcome(name): print("Welcome to tripplanner v1.0 " + name) trip_planner_welcome("Simon") def estimated_time_rounded(estimated_time): rounded_time = round(estimated_time) return rounded_time estimate = estimated_time_rounded(5.5) 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("Germany", "Slovenia", estimate, "Car")

This will estimate the right time but when I change the time to 3.5 or 6.5 it will estimate the wrong time.

def trip_planner_welcome(name): print("Welcome to tripplanner v1.0 " + name) trip_planner_welcome("Simon") def estimated_time_rounded(estimated_time): rounded_time = round(estimated_time) return rounded_time estimate = estimated_time_rounded(6.5) 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("Germany", "Slovenia", estimate, "Car")

An explanation would be appreciated.

1 Like

Something I did not know until you brought up this question, and for which one can only speculate (at present) a possible reason, assuming one is brave enough. I’m not.

for x in range(1, 21):
    print (round(x + 0.5))

    
2
2
4
4
6
6
8
8
10
10
12
12
14
14
16
16
18
18
20
20

It only seems to be rounding to even integers. Weird, and desperately in need of further research. It will give you practice reading Python docs. Please report back with your findings.

2 Likes

Thank you for your reply.
I looked it up and found out, quote “if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2 ).”

1 Like

Why is this telling me that I need I am not defining the default value of the parameter when the default value section stated that a default value is where we give our function parameters values that using the assignment operator ( = ) and that is should happen in the function declaration?

When I write this


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

  1. I get told I’m not defining the mode of transport. Yet it says you can define a function parameter by assigning the ( = )?

  2. Also, why would we need to make any of these strings if the only one with an integer will be estimated time?

`

Sorry I rewrote the code to this after understanding what it was saying further but its still telling me mode of transport isnt defined and wont run.

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

trip_planner_welcome("Jimel")

def estimated_time_rounded(estimated_time):
 rounded_time = round(estimated_time)
 return rounded_time
estimate = estimated_time_rounded(2.35)

def destination_setup(orgin, destination, estimated_time,mode_of_transport="Car"):
  print("Your trip starts in 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("Alaska " + "New Jersey " + estimate + "Car")

The automated system doesn’t always identify the possible error correctly. The error message may show a different error than the actual problem.

In the code you have posted, consider the following issues:

  • As per Step 4:
# Expected Output:
"Your trip starts off in <origin>"

# Your Output:
"Your trip starts in in <origin>"
  • As per Step 4:
# Expected Output:
"It will take approximately <estimated_time> hours"

# Your Output:
"It will take approximately <estimated_time>hours"
  • You have a typo in your first parameter i.e. you wrote orgin instead of origin.

  • Your function call is incorrect.

# You wrote:
destination_setup("Alaska " + "New Jersey " + estimate + "Car")

# It should be:
destination_setup("Alaska", "New Jersey", estimate, "Car")
2 Likes

Butting in: Given that ‘Car’ is the default, it could be,

destination_setup("Alaska", "New Jersey", estimate)

Just saying, (grin).

2 Likes

This makes me want to ask why could it be the latter in terms of the execution of the function… Without it being in the function call how could it be that way?

Thanks a lot for this! I guess I assumed that because ti was multiple it would need to be expressed as you would if printing. Im assuming then with functions its not possible to do them with ( + ) and it will always be multiple returns separated by ( , ) ?

The purpose of a default parameter is to make the positional argument optional. If the argument is omitted (from the call), the default value is used inside the function.

1 Like

Thanks so much. One more question. I’m still receiving an error in Step 4.

I keep getting this:

Did you use print() with the string "Your trip starts off in " concatenated with the parameter origin ?

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

trip_planner_welcome("Jimel")

def estimated_time_rounded(estimated_time):
 rounded_time = round(estimated_time)
 return rounded_time
estimate = estimated_time_rounded(2.35)

def destination_setup(origin, destination, estimated_time, mode_of_transport= "Car"):
  print("Your trip starts in " + str(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("Alaska ","New Jersey ",  estimate , "Car")

Check your text against the instructions. They must match exactly.

1 Like

Just caught it after I posted thank you so much for your help. For further clarification so I understand the concept. I removed “Car” from the function call it informs me that when I do so I need to have it written in the function call. Why is it telling me that if default values make the positional argument and it was written in inside the print portion of the function (inside the function itself).

Is this just because of how the exercise is written? This happens in step 3.

"Car" is not needed in the call since that is the default parameter. It might be that the lesson checker doesn’t like the space after the =?

There is no need to cast a str() from origin since it is already a string.

Not sure I follow your question, above. Please show us the error message.

Remember to include a space.

When I remove str I get this error.