-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit-converter.py
More file actions
80 lines (59 loc) · 2.34 KB
/
unit-converter.py
File metadata and controls
80 lines (59 loc) · 2.34 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from hashlib import new
print('''
Unit Options:
1. feet
2. miles
3. meters
4. kilometers
''')
#Version 1
## Ask the user for the number of feet, and print out the equivalent distance in meters
# one_feet = 0.3048
# number_of_distance = float(input("What is the number of distance?: "))
# result = round(number_of_distance * one_feet, 3)
#print(f"{number_of_distance} ft is {result} m")
#=====================================================================================================#
#Version 2
## Allow the user to also enter the units. Then depending on the units, convert the distance into meters.
metrics = {
"feet": 0.3048,
"miles": 1609.34,
"meters": 1,
"kilometers": 1000,
"ft": 0.3048,
"mi": 1609.34,
"m": 1,
"km": 1000
}
# user_input_unit = input("What are the units?: ").lower()
# if user_input_unit in metrics:
# print(f'{int(number_of_distance)} {user_input_unit} is {int(number_of_distance * metrics[user_input_unit])} m ')
# else:
# print("Input error! please try again")
#=====================================================================================================#
# Version 3
## Add support for yards, and inches.
metrics["yard"] = 0.9144
metrics["inch"] = 0.0254
# user_input_unit = input("What are the input units?: ").lower()
# # if user_input_unit in metrics:
# # print(f'{int(number_of_distance)} {user_input_unit} is {int(number_of_distance * metrics[user_input_unit])} m ')
# # else:
# # print("Input error! please try again")
# #=====================================================================================================#
#Version 4
## ask the user for the distance, the starting units, and the units to convert to.
while True:
try:
number_of_distance = float(input("What is the number of distance?: "))
except:
print ("Input error! please input number only")
continue
user_input_unit = input("What are the input units?: ").lower()
user_output_unit = input("What are the output units?: ").lower()
if user_input_unit in metrics and user_output_unit in metrics:
new_value = round(number_of_distance * metrics[user_input_unit]/ metrics[user_output_unit], 5)
print(f'{number_of_distance} {user_input_unit} is {new_value} {user_output_unit}')
break
else:
print("Input Error! Please try again")