I'm trying to write a function sum_Roman that takes integers and outputs a summation table of Roman numbers

Question:

The question itself is two parts, first part is to make a function of int_to_roman(num) that converts integers into their roman numerical counterparts, just displaying them. The second part is to use that code and make a function called sum_roman that takes a decimal integer as an input and returns the summation table of roman numbers corresponding to the decimal integers from 1 up to the number inputted.

I did a code for the first part in the form of:

class py_solution:


    def int_to_Roman(self, num):
        val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
        syb = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
        roman_num = ''
        a = 0
        while  num > 0:
            for _ in range(num // val[a]):
                roman_num += syb[a]
                num -= val[a]
            a += 1
       return roman_num

This code worked for the first part giving me the roman numerials but I haven’t been able to find anything that works for part 2, as I don’t know the code to calculate all roman numerial up to that point or to make them into a table.

Asked By: Spuddy

||

Answers:

Don’t you just need a for loop?

def int_to_Roman( num):
    val = [1000, 900, 500, 400,
        100, 90, 50, 40,
        10, 9, 5, 4,
        1]
    syb = ["M", "CM", "D", "CD",
        "C", "XC", "L", "XL",
        "X", "IX", "V", "IV",
        "I"]
    roman_num = ''
    a = 0
    while  num > 0:
        for _ in range(num // val[a]):
            roman_num += syb[a]
            num -= val[a]
        a += 1
    return roman_num

    
def sum_roman(final):

    for i in range(final):
        decimal = 1+i # Start at 1 (not 0) and end at final (not final-1)
        roman = int_to_Roman(decimal)
        print(f"{decimal:6.0f}   {roman}")

sum_roman(10)

This gives:

     1   I
     2   II
     3   III
     4   IV
     5   V
     6   VI
     7   VII
     8   VIII
     9   IX
    10   X

I don’t know why you call this a "summation" table

Perhaps you mean a "summary" table?

I have not done any summation for you as I think you asked for that in error. But you are free to do summing, instead of summarising, if you wish.

Thank you for giving the exact wording of the task set for you. It does not say "summation".

Answered By: Eureka
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.