-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroman_to_int.py
52 lines (41 loc) · 1.2 KB
/
roman_to_int.py
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
def roman_to_int(numeral):
'''
Function to convert numerals to integers
'''
final_answer = 0
if "CM" in numeral:
final_answer += 900
numeral = numeral.replace("CM", "")
elif "CD" in numeral:
final_answer += 400
numeral = numeral.replace("CD", "")
elif "XC" in numeral:
final_answer += 90
numeral = numeral.replace("XC", "")
elif "XL" in numeral:
final_answer += 40
numeral = numeral.replace("XL", "")
elif "IX" in numeral:
final_answer += 9
numeral = numeral.replace("IX", "")
elif "IV" in numeral:
final_answer += 4
numeral = numeral.replace("IV", "")
for i in numeral:
if i == "M":
final_answer += 1000
elif i == "D":
final_answer += 500
elif i == "C":
final_answer += 100
elif i == "L":
final_answer += 50
elif i == "X":
final_answer += 10
elif i == "V":
final_answer += 5
elif i == "I":
final_answer += 1
print("The Roman numerals you entered translate to: " + str(final_answer) + "!")
numeral_input = input("Please enter the Roman numerals you would like to convert: ")
roman_to_int(numeral_input)