Round Numbers (Solution)
To solve this exercise, we need to first ask the user to enter a number by calling the
input() function with a prompt message. As you remember,
input() always returns a string value, which must be converted to a number before we can do anything useful with it—that is, in the mathematical domain.
Our goal is to round the number to two decimal places. Previously, we’ve only formatted the number without changing the number itself. To obtain a new rounded value, we can pass it to the built-in
round() function, which truncates the fractional part, leaving only the whole number in front of the decimal point.
Let’s save it and run it again. It’s always a good idea to verify if your program really does what it’s supposed to do. Okay, this is working fine. Let’s try something else. This time I’m going to type a different number,
1.789, which should give me a number rounded up.
Become a Member to join the conversation.