forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman_numerals.py
27 lines (24 loc) · 954 Bytes
/
roman_numerals.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
# -*- coding: utf-8 -*-
lettersDict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
roman = list(input('Enter a roman numeral: ').upper())
valid = 1
for letter in roman:
if letter not in lettersDict:
valid = 0
print('Not a valid roman numeral.')
break
if valid == 1:
result = 0
previous_letter = 'M'
for letter in roman:
current_letter = roman[roman.index(letter)]
if list(lettersDict.keys()).index(current_letter) > list(lettersDict.keys()).index(previous_letter):
number = lettersDict.get(current_letter)
result =- lettersDict.get(previous_letter)
number = lettersDict.get(current_letter) - lettersDict.get(previous_letter)
result =+ number
else:
number = lettersDict.get(current_letter)
result += number
previous_letter = current_letter
print(result)