-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path007-abs.py
More file actions
28 lines (24 loc) · 819 Bytes
/
007-abs.py
File metadata and controls
28 lines (24 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
Get the absolute value of a number
----------------------------------
Input: number
Output: absolute value of number
"""
numbers = [1, 0.5, 0, -0.5, -1]
for i in numbers:
abs_i = abs(i)
print('original number: {} <-> absolute value: {}'.format(i, abs_i))
number = input('Give a number: ')
number = float(number)
print('original number: {} <-> absolute value: {}'.format(number, abs(number)))
print('')
# How can you code this process without using abs() function?
number = input('Give a number: ')
number = float(number)
# NOTE: There is no need o check number == 0 condition, since it is equal to
# the else
if number < 0:
abs_number = number * -1
else:
abs_number = number
print('original number: {} <-> absolute value: {}'.format(number, abs_number))