FAQ: Introduction to Functions - Review

On step 4 of review, where we write the estimated_time_rounded function. The lesson says to "Print rounded_time", but you need to return rounded_time to successfully complete the step.

2 Likes

I ran into the same problem.

I finished the Review exercise for Intro to Function and received green checks for each step of the review with no issues. However, the program does not run. Instead, I get the following error:

Traceback (most recent call last):
File “travel.py”, line 18, in
trip_planner_welcome(Chris)
NameError: name ‘Chris’ is not defined

Appreciate any feedback or suggestions here.

Welcome to the forums!

The error suggests that the variable Chris was not defined. You should define and instantiate this variable before passing it to a function.

I’m so confused about why I’m not receiving any output for the code, even though the check mark shows green for my last step. I’m not getting an error either. Does anyone see any issues with my code below?

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

def destination_setup(origin="Chicago", destination="Italy", 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")

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

Is that all the code? There are function definitions but nothing is called. If you’re adding code to the forums please see- How do I format code in my posts?

Awww…I see. Thanks for the input.

On this review, it says that 2.43 would represent 2 hours and 43 minutes. This should round to 3 hours. I am wondering what would be the best way to handle this. With my current knowledge of Python, I would do this.

rounded_time = round(int(estimated_time) + ((estimated_time - int(estimated_time)) * 60))
If using 2.43, this would be rounded_time = round(2 + (2.43 - 2)*60)

I would love to hear other ideas.

Welcome to the forums!

This actually won’t work. Try plugging in 2.43. You’d expect a result of 3, instead, you get 28. Can you spot the problem and fix it?

Thank you for the feedback.
rounded_time = round(int(estimated_time) + ((estimated_time - int(estimated_time)) /0.6))

Yep, looks great!

If “.43 represents 43 minutes,” I don’t understand why the program rounds to 2 hours instead of three, does anyone have an idea?

This is because 2.43 is less than 2.5 so the closest whole integer is 2 per documentation on round(). Check the above discussion to see a solution for rounding to the nearest hour based on how minutes actually work.

3 Likes

Why is the following piece of code not executed?

def estimated_time_rounded(estimated_time=2.5): rounded_time = round(estimated_time) return rounded_time estimated_time_rounded()

Full Code

def trip_planner_welcome(name): print("Welcome to tripplanner v1.0 " + name) trip_planner_welcome("Kenny") def destination_setup(origin="Mexico", destination="Monterey", estimated_time=2.5, 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(estimated_time) return rounded_time estimated_time_rounded()

Since there is no error throw it seems likely that it was executed. If you’re looking for visual feedback consider printing what your function returns, e.g. print(estimated_time_rounded())

1 Like

thanks, I didn’t know you could print the function call :clap:

1 Like

In most cases you’ll find functions use return and not print at typically you want to pass objects around within your script, not get text output.

But for learning and debugging print is great. Use it as you will, but make sure your function returns if you want to actually make use of the value it returns :+1:.

1 Like

I got the time round this way, converting it to minutes first. Anyway, makes sense to me… :slight_smile:

def estimated_time_rounded(time):

minutes = (time % 1)*100 + (int(time)*60)

rounded_time = round(minutes/60)

return rounded_time

2 Likes

This is my code from step 4. From my understanding and from comparing it with the Solution page, there’s nothing wrong with the code. However, the program is reviewing that the code has “IndentationError: expected an indented block”. Does anyone see the Indentation Error so I can avoid it in the future?

# 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(estimated_time)
  return rounded_time

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