The examples in this article describe how Python converts Arabic numerals to Roman numerals. Share to everyone for your reference. The implementation method is as follows:
def numtoromannum (num): "" "digital would be converted into Roman numerals,ex:numtoromannum (3999)" "" If num < 1 or num > 3999:print ' The Num must in 1-3999 ' Else:numdic = {' 1 ':(' I ', ' IV ', ' V ', ' IX '), ' 2 ':(' X ', ' XL ', ' L ', ' XC '), ' 3 ':(' C ', ' CD ', ' D ', ' CM '), ' 4 ':(' M ')} items = sorted (Numdic.items ()) Retstr = "fo R item in ITEMS:STR = "(num,modnum) = Divmod (num,10) if modnum! = 0:if item[0]! = ' 4 ': If Modnum <= 3:while modnum > 0:str = str.join ([' ', Item[1][0]]) modnu M-= 1 Elif modnum < 5:STR = Item[1][1] elif Modnum = = 5:str = item[1][2] Elif Modnum < 9:STR = item[1][2] while modnum > 5:str = Str.join ( [", Item[1][0]]) Modnum-= 1 else:str = item[1][3] Else:while modnum > 0:sTR = Str.join ([', item[1][0]]) modnum-= 1 RETSTR = str.join ([' ', Retstr]) return RETSTR
Hopefully this article will help you with Python programming.