-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint_integer_Number_of_roman_numeral.py
More file actions
30 lines (24 loc) · 1.18 KB
/
Print_integer_Number_of_roman_numeral.py
File metadata and controls
30 lines (24 loc) · 1.18 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
################################The Main Logic is################################
# here I convert the roman numbers to its equivalent integer number. At first I reversed the roman number entered by user and store some roman value in a dictionary. After reversed the roman number I store the last roman integer in a variable and do iteration through loop and check if the value of second last roman value is less than the last roman value then subtract the value of second last roman value from the total number or result else if the value of second last roman value is greater than the last roman value then add the value of second last roman value to the total number or result
def convertToInt(s):
roman_number = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
total = 0
last_value = 0
for result in reversed(s):
value = roman_number[result]
if value < last_value:
total -= value
else:
total += value
last_value = value
return total
s = input("Enter Roman Number: ")
print(f"The Integer Number is: {convertToInt(s)}")