FAQ: Introduction to Functions - Built-in Functions vs User Defined Functions

This community-built FAQ covers the “Built-in Functions vs User Defined Functions” 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 Built-in Functions vs User Defined Functions

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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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!

Just learning how to call the built-in round() function and the documentation from the python link appears to disagree with the output I got in the exercise. I’m checking to see if it’s a bug or if I’m just misunderstanding the note in the documentation. Please excuse my ignorance with regard to how to format posts like these.

For:
tshirt_price = 9.75

rounded_price = round(tshirt_price, 1)

print(rounded_price)

Run returns the value 9.8

From the provided notation, it appears as though round() doesn’t actually round to the nearest decimal but simply truncates the float value at the designated decimal place.

Here’s the direct quote from Built-in Functions — Python 3.10.0 documentation

" Note: The behavior of [ round() ] for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68 . This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float."

Thank you for your help!
Mark

1 Like

The problem would be that it can be inconsistent at times when you don’t expect it to be. The given example used with 2.675 on my system cannot be stored accurately and is evaluated to something like-
2.67499999999999982236 ...
Now the ‘rounding’ to 2.67 there makes a little bit more sense, aye?

On the other hand 9.75 can be stored accurately, 9.7500000000000000 ... so the rounding makes sense again.

If you’re entirely unfamiliar with the way values can be stored in binary then it might be worth a quick diversion. The python docs have a nice description of floating point issues (and some possible workarounds): 15. Floating Point Arithmetic: Issues and Limitations — Python 3.10.0 documentation but it does assume you know a little about binary integers/floats/doubles etc.

2 Likes

Thanks for the quick and thorough reply, @tgrtim. Seems pretty evident this one’s over my head at the moment and it’s probably better to just take it at face value until I’m familiar enough with the basics to venture out into the unknown. This is my third time coming to codecademy (in more years than I’d like to admit) to learn some coding essentials, so I really want to do my best not to overwhelm myself this time.

Thanks again!
Mark

1 Like

Where it says “round the price of the variable tshirt_price by one decimal place.” – surely it would be more normal to use the preposition ‘to’ to express the precision required; I found ‘by’ rather confusing?

Hello! Please forgive me if this is not the right place to ask this question. In this exercise, I assigned the 4 items to a list (just for practice) and it works with the min and max call, but not for the round function. Specifically, I put the four variables into a list called prices. Then I called them by using: rounded_price = round(prices), which did not work. Is there a way to write this?

We’re not really sure which thingamajig you’re referring to. Please post a link to the exercise so we may gain some context.

Sorry about that! I found an answer though. If you’re trying to “round” all items in a list, you can use a statement such as:
rounded_numbers = [round(item) for item in my_list]

1 Like

Glad you solved that. Bear in mind that the above will set everything to the nearest integer.

>>> round(1.5)
2
>>> round(1.4)
1
>>> from math import pi
>>> round(pi, 4)
3.1416
>>> 

A post was split to a new topic: How can I stay motivated?