Home > Blockchain >  Convert from numbers to roman with input
Convert from numbers to roman with input

Time:11-24

hello devs i'm new to python and i just want to convert from numbers to roman, i know there is already but i don't understand how to do it here my code :

t = int(input("repeat : "))
for i in range(t):
    try:
        n = int(input(""))
    except ValueError:
        print("incorrect input")
    
romnumber = { 1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL',
              50: 'L', 90: 'XC', 100: 'C', 400: 'XD', 500: 'D', 900: 'CM', 1000: 'M'}

print_order = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]

for x in print_order:
    if n != 0:
        quotient= n//x
        #If quotient is not zero output the roman equivalent
        if quotient != 0:
            for y in range(quotient):
                print(romnumber[x], end="")

        #update integer with remainder
        n = n%x

i want to input be like this:

repeat: 2
1
3

and output:

I
III

but right now its only save last input don't know what to do to get 2 or more inputs for converting number to roman numbers

CodePudding user response:

You need to perform your operation inside the loop, or maybe save the input:

t = int(input("repeat : "))

romnumber = { 1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL',
              50: 'L', 90: 'XC', 100: 'C', 400: 'XD', 500: 'D', 900: 'CM', 1000: 'M'}

print_order = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]

inputs = []
for i in range(t):
    try:
        inputs.append(int(input("")))
    except ValueError:
        print("incorrect input")

for n in inputs:
    for x in print_order:
        if n != 0:
            quotient= n//x
            #If quotient is not zero output the roman equivalent
            if quotient != 0:
                for y in range(quotient):
                    print(romnumber[x], end="")

            #update integer with remainder
            n = n%x
    print()
  • Related