FAQ: Introduction to Functions - Review

This community-built FAQ covers the “Review” exercise from the lesson “Introduction to Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Visualize Data with Python
Data Scientist
Analyze Data with Python
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Data Analyst

Learn Python 3
CS101 Livestream Series

FAQs on the exercise Review

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 (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 (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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.

4 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

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