Basic program to convert integer to Roman numerals?

后端 未结 24 1301
孤独总比滥情好
孤独总比滥情好 2020-11-30 11:52

I\'m trying to write a code that converts a user-inputted integer into its Roman numeral equivalent. What I have so far is:

The point of the generate_

24条回答
  •  無奈伤痛
    2020-11-30 12:37

    The code for this roman numeral does not check for errors like wrong letters it is just for a perfect roman numeral letters

    roman_dict = {'M':1000, 'CM':900, 'D':500, 'CD':400, 'C':100, 'XC':90,
             'L':50, 'XL':40, 'X':10, 'IX':9, 'V':5, 'IV':4,'I':1}
    
    roman = input('Enter the roman numeral: ').upper()
    roman_initial = roman # used to retain the original roman figure entered
    
    lst = []
    
    
    while roman != '':    
    if len(roman) > 1:
        check = roman[0] + roman[1]
        if check in roman_dict and len(roman) > 1:
            lst.append(check)
            roman = roman[roman.index(check[1])+1:]
        else:
            if check not in roman_dict and len(roman) > 1:
                lst.append(check[0])
                roman = roman[roman.index(check[0])+1:]   
    else:
        if len(roman)==1:
            check = roman[0]
            lst.append(check[0])
            roman = ''
    
    if lst != []:
    Sum = 0
    for i in lst:
        if i in roman_dict:
            Sum += roman_dict[i]
    print('The roman numeral %s entered is'%(roman_initial),Sum)
    

提交回复
热议问题