Calculate the Absolute Value (Solution)
00:00
Okay. The first instruction is virtually identical to the one from the previous exercise, so this one should be fairly easy. You define a variable called num and assign a floating-point number obtained from the user. So, enter a number.
00:20
Now, you can print the message onto the screen using an f-string: f"The absolute value of {num} is {}"
00:29
and here inside the second pair of curly brackets, you can write abs(), which is short for absolute value, and you pass your number. Now, let’s save and reload it. Let’s enter -10, as in the example. Great.
00:46 You’re getting the intended result. You solved this exercise quickly. The next exercise may be a bit more challenging.
Bartosz Zaczyński RP Team on May 14, 2025
@Andrii Dashchakovskyi Sharp eye! The formatting wasn’t the point of this exercise, but you’re right. One way to keep the original value the user has supplied could be the following:
>>> user_input = input("Enter a number: ")
Enter a number: -10
>>> print(f"The absolute value of {user_input} is {abs(float(user_input))}")
The absolute value of -10 is 10.0
This will produce the expected output for the given input. Alternatively, you could format both numbers to your liking explicitly:
>>> num = float(input("Enter a number: "))
Enter a number: -10
>>> print(f"The absolute value of {num:.3f} is {abs(num):.3f}")
The absolute value of -10.000 is 10.000
All approaches are valid. It just depends on whether you want to preserve the raw input or apply consistent number formatting.
Become a Member to join the conversation.

Andrii Dashchakovskyi on May 14, 2025
The output of print in the solution is a bit different from what is asked in the exercise.