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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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.
" 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."
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.75can 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.
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.
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?
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]