vendredi 25 septembre 2020

Convert a for loop with conditions from c to python

Variables:

n = 378282246310005
m = n
i = 1
sum1 = 0
sum2 = 0
sum11 = 0

I want to convert the following c-code to python-code:

for (m = n, i = 1; m != 0; m = m / 10, i++)
    {
        r = m % 10;
        if (i % 2 != 0)
        {
            sum1 += r;
        }
        else
        {
            sum2 += r * 2;
        }
        while (sum2 > 0)
        {
            z = sum2 % 10;
            sum11 = sum11 + z;
            sum2 = sum2 / 10;
        }
    }

I came up with the following code for python, but it does not work:

for m in range(m!=0):

    r = m % 10

    if (i % 2 != 0):
        sum1 += r

    else:
        sum2 += r * 2

    while sum2 > 0:
        z = sum2 % 10
        sum11 = sum11 + z
        sum2 = sum2 / 10

    m = m / 10
    i = i + 1

Where do I have to change the logic or the code, for it to work?

Aucun commentaire:

Enregistrer un commentaire