import re subnums = [ ('cd',4*'c'), ('xl',4*'x'), ('iv',4*'i'), ('d',5*'c'), ('l',5*'x'), ('v',5*'i'), ('cm',9*'c'), ('xc',9*'x'), ('ix',9*'i')] nums = [ ('m',1000), ('c',100), ('x', 10), ('i',1) ] def cal(x): for (s, l) in subnums: x = x.replace(s, l) x = '+'.join(list(x)) for (c, w) in nums: x = x.replace(c, str(w)) if x == '': x = '0' return eval(x) def ask(): roman = raw_input('Input the Roman Number: ').lower() if (re.search("^m*?(d|cm|cd)?c*?(l|xc|xl)?x*?(ix|v|iv)?(i{1,3})?$",roman)): print " The Roman number %s is actually %s" % (roman.upper(), cal(roman)) else: print " This is definately not a Roman Number." print " Please pay attention." ask() ask()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question