def toRoman(n): if n<1 or n>10: return "ERR" # our result result = "" while n>0: if n>=10: result += "X" # we add the symbol to our result n -= 10 # we decrement the number by the value of the symbol # this is where it gets interesting # at each level, if you allow for 9s and 4s, you're good elif n==9: result += "IX" n -= 9 elif n>=5: result += "V" n -= 5 elif n==4: result += "IV" n -= 4 else: result += "I" n -= 1 return result print([(i,toRoman(i)) for i in range(42)])
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