-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman_to_intiger.py
More file actions
55 lines (42 loc) · 877 Bytes
/
roman_to_intiger.py
File metadata and controls
55 lines (42 loc) · 877 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
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
from sys import exit
def check(c):
v = 0
if c == "I":
v = 1
elif c == "V":
v = 5
elif c == "X":
v = 10
elif c == "L":
v = 50
elif c == "C":
v = 100
elif c == "D":
v = 500
elif c == "M":
v = 1000
return v
def main():
s = input("Enter a roman numeral : ")
c1 = 0
c2 = 0
ans = 0
for c in s:
c1 = check(c)
if c1 == 0:
print("You entered a wrong roman numeral")
exit(1)
for ch in range(len(s)):
c1 = check(s[ch])
if ch + 1 < len(s):
c2 = check(s[ch + 1])
if c1 >= c2:
ans += c1
else:
ans = ans + c2 - c1
ans = ans - c2
ch += 2
else:
ans += c1
print(f"Answer:{ans}")
main()