Conversion of integers to Roman numerals (python)
<p>I understand there were many similar questions being asked on this topic. But I still have some doubts need to be clear.</p>

<pre><code>def int_to_roman(input):

    if type(input) != type(1):
        raise TypeError, "expected integer, got %s" % type(input)
    if not 0 &lt; input &lt; 4000:
        raise ValueError, "Argument must be between 1 and 3999"   
    ints = (1000, 900,  500, 400, 100,  90, 50,  40, 10,  9,   5,  4,   1)
    nums = ('M',  'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
    result = ""
    for i in range(len(ints)):
        count = int(input / ints[i])
        result += nums[i] * count
        input -= ints[i] * count
    return result
</code></pre>

<p>I dont really understand the code below:</p>

<pre><code>for i in range(len(ints)):
    count = int(input / ints[i])
    result += nums[i] * count
    input -= ints[i] * count
</code></pre>

<p>Does:</p>

<pre><code>for i in range (len(ints)):
</code></pre>

<p>it means '1000','900','800' (the integers respectively) or it means 13 (13 integers in integers)?</p>

<pre><code>count = int(input / ints[i])
</code></pre>

<p>what does ints [i] mean?</p>

<p>Anyone please explain these codes? It would be best if you could show the examples (like substitute numbers and show how it works)</p>

<p>The two lists, <code>ints</code> and <code>nums</code> are the same length.  The loop iterates over the length of <code>ints</code>, which means that the variable <code>i</code> can access the same position of either list, matching one to the other.</p>

<p>If we step through the loop, <code>count</code> is assigned the integer value of the input divided by the first number in <code>ints</code>, which is 1000.  If the <code>input</code> variable is, say, 10, then 10/1000 will result in a number &lt;1, and using <code>int()</code> on the result will cause it to assign 0 to <code>count</code>.  When 0 is multiplied by the matching string from <code>nums</code>, the assigned result is basically nothing.  Then the same amount is subtracted from <code>input</code>, which in this case leaves it unchanged.</p>

<p>Eventually, the loop will reach a point when the result of the division is a number >=1, so the following steps will do something.</p>

<p>Let's say the result of <code>int(input / ints[i])</code> is 3.  <code>"X" * 3</code> results in <code>"XXX"</code>, which is added to <code>result</code>, and the <code>input</code> is reduced by the appropriate amount, in this case 30.  So on, until the loop ends.</p>

python python-2.7 function roman-numerals