Basic program to convert integer to Roman numerals?

后端 未结 24 1372
孤独总比滥情好
孤独总比滥情好 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:26

     """
    # This program will allow the user to input a number from 1 - 3999 (in english) and will translate it to Roman numerals.
    # sources: http://romannumerals.babuo.com/roman-numerals-100-1000
    Guys the reason why I wrote this program like that so it becomes readable for everybody. 
    Let me know if you have any questions... 
    """
    while True:
        try:
            x = input("Enter a positive integer from 1 - 3999 (without  spaces) and this program will translated to Roman numbers: ")
            inttX = int(x)
            if (inttX) == 0 or 0 > (inttX):
                print("Unfortunately, the  smallest number that you can enter is 1 ")
            elif (inttX) > 3999:
                print("Unfortunately, the  greatest number that you can enter is 3999")
            else:
                if len(x) == 1:
                    if inttX == 1:
                        first = "I"
                    elif inttX == 2:
                        first = "II"
                    elif inttX == 3:
                        first = "III"
                    elif inttX == 4:
                        first = "IV"
                    elif inttX == 5:
                        first = "V"
                    elif inttX == 6:
                        first = "VI"
                    elif inttX == 7:
                        first = "VII"
                    elif inttX == 8:
                        first = "VIII"
                    elif inttX == 9:
                        first = "IX"
                    print(first)
                    break
                if len(x) == 2:
                    a = int(x[0])
                    b = int(x[1])
                    if a == 0:
                        first = ""
                    elif a == 1:
                        first = "X"
                    elif a == 2:
                        first = "XX"
                    elif a == 3:
                        first = "XXX"
                    elif a == 4:
                        first = "XL"
                    elif a == 5:
                        first = "L"
                    elif a == 6:
                        first = "LX"
                    elif a == 7:
                        first = "LXX"
                    elif a == 8:
                        first = "LXXX"
                    elif a == 9:
                        first = "XC"
    
                    if b == 0:
                        first1 = "0"
                    if b == 1:
                        first1 = "I"
                    elif b == 2:
                        first1 = "II"
                    elif b == 3:
                        first1 = "III"
                    elif b == 4:
                        first1 = "IV"
                    elif b == 5:
                        first1 = "V"
                    elif b == 6:
                        first1 = "VI"
                    elif b == 7:
                        first1 = "VII"
                    elif b == 8:
                        first1 = "VIII"
                    elif b == 9:
                        first1 = "IX"
                    print(first + first1)
                    break
                if len(x) == 3:
                    a = int(x[0])
                    b = int(x[1])
                    c = int(x[2])
                    if a == 0:
                        first12 = ""
                    if a == 1:
                        first12 = "C"
                    elif a == 2:
                        first12 = "CC"
                    elif a == 3:
                        first12 = "CCC"
                    elif a == 4:
                        first12 = "CD"
                    elif a == 5:
                        first12 = "D"
                    elif a == 6:
                        first12 = "DC"
                    elif a == 7:
                        first12 = "DCC"
                    elif a == 8:
                        first12 = "DCCC"
                    elif a == 9:
                        first12 = "CM"
    
                    if b == 0:
                        first = ""
                    elif b == 1:
                        first = "X"
                    elif b == 2:
                        first = "XX"
                    elif b == 3:
                        first = "XXX"
                    elif b == 4:
                        first = "XL"
                    elif b == 5:
                        first = "L"
                    elif b == 6:
                        first = "LX"
                    elif b == 7:
                        first = "LXX"
                    elif b == 8:
                        first = "LXXX"
                    elif b == 9:
                        first = "XC"
    
                    if c == 1:
                        first1 = "I"
                    elif c == 2:
                        first1 = "II"
                    elif c == 3:
                        first1 = "III"
                    elif c == 4:
                        first1 = "IV"
                    elif c == 5:
                        first1 = "V"
                    elif c == 6:
                        first1 = "VI"
                    elif c == 7:
                        first1 = "VII"
                    elif c == 8:
                        first1 = "VIII"
                    elif c == 9:
                        first1 = "IX"
                    print(first12 + first + first1)
                    break
    
                if len(x) == 4:
                    a = int(x[0])
                    b = int(x[1])
                    c = int(x[2])
                    d = int(x[3])
                    if a == 0:
                        first1 = ""
                    if a == 1:
                        first1 = "M"
                    elif a == 2:
                        first1 = "MM"
                    elif a == 3:
                        first1 = "MMM"
    
                    if b == 0:
                        first12 = ""
                    if b == 1:
                        first12 = "C"
                    elif b == 2:
                        first12 = "CC"
                    elif b == 3:
                        first12 = "CCC"
                    elif b == 4:
                        first12 = "CD"
                    elif b == 5:
                        first12 = "D"
                    elif b == 6:
                        first12 = "DC"
                    elif b == 7:
                        first12 = "DCC"
                    elif b == 8:
                        first12 = "DCCC"
                    elif b == 9:
                        first12 = "CM"
    
                    if c == 0:
                        first3 = ""
                    elif c == 1:
                        first3 = "X"
                    elif c == 2:
                        first3 = "XX"
                    elif c == 3:
                        first3 = "XXX"
                    elif c == 4:
                        first3 = "XL"
                    elif c == 5:
                        first3 = "L"
                    elif c == 6:
                        first3 = "LX"
                    elif c == 7:
                        first3 = "LXX"
                    elif c == 8:
                        first3 = "LXXX"
                    elif c == 9:
                        first3 = "XC"
    
                    if d == 0:
                        first = ""
                    elif d == 1:
                        first = "I"
                    elif d == 2:
                        first = "II"
                    elif d == 3:
                        first = "III"
                    elif d == 4:
                        first = "IV"
                    elif d == 5:
                        first = "V"
                    elif d == 6:
                        first = "VI"
                    elif d == 7:
                        first = "VII"
                    elif d == 8:
                        first = "VIII"
                    elif d == 9:
                        first = "IX"
                    print(first1 + first12 + first3 + first)
                    break
        except ValueError:
            print(" Please enter a positive integer! ")
    

提交回复
热议问题