mercredi 30 novembre 2016

A while loop in my program has multiple ifs, but never reaches the bottom one?

I am in the middle of a really difficult to grasp assignment for school. I've been trying for a few days and I think I'm finally almost done, but... I'm stuck again and don't know why.

I am supposed to let the user fill in a number invoer (Dutch)/input. That number is supposed to be smaller than, or equal to 50. This number determines how many more inputs the user can do - these new inputs are test cases. Each test case should be a number larger than 0, but equal to, or lower than 100. It is not required to go very deep into handling bad input, so I only use some basic handling for that.

Then what needs to be tested for each test case, is what the smallest number is that is divisible by the test case, but the sum of the separate digits of that smallest number should equal your test case itself. So in the example they state: input = 1, (which means 1 test case, but they dont state that), input 2 = 10 (which means your only test case is the number 10, but they dont state that). What it sends out is the number 190 (this is because 190/10 = 19, so it's a correct outcome, but 1+9+0 = 10 as well, so it passes the second test, but again, they dont state that. I had to figure this all out myself, which is why the assigment takes me so long... the question itself is in Dutch and barely gives you any information at all).

So what my program DOES test, if I input number 25 as test case for example, that 25/25 = 1, so valid and 2+5 = 7. However, after calculating this number 7, it never seems to check the second condition. The sum of the digits needs to be 25, not 7. The program gets thrown in an infinite loop now and I don't see what is wrong.

I will post my code below. I apologize for the variable names and printed text, as they are in Dutch. I hope I explained the meaning of all this clearly enough.

import sys

invoer = int(input('Vul het aantal testgevallen in: '))
if invoer <= 50:
    invoer = invoer
else:
    print('Het getal moet groter kleiner dan of gelijk aan 50 zijn!!')
    sys.exit()

t = 0
testgevallen = []

while t < invoer:

    invoer2 = int(input('Vul een testgeval in groter dan 0 en kleiner of gelijk aan 100: '))
    if 0 < invoer2 <= 100:
        print('Testgeval ', t + 1, 'is: ', invoer2)
        testgevallen.append(invoer2)
        t += 1
        print('Array is ', testgevallen)
    else:
        print('Het getal moet groter zijn dan 0 en kleiner of gelijk aan 100!!')
        sys.exit()
t = 0

while t < invoer:
    print('Opgegeven nummer ', t + 1, ' is ', testgevallen[t])
    vermenigvuldiging = 1

    doorgaan = True

    while doorgaan == True:
        getal = testgevallen[t] * vermenigvuldiging

        if testgevallen[t] % getal == 0:
            print(getal, ' - Dit getal is deelbaar door ', testgevallen[t])

            som = sum(map(int, str(getal)))
            print(som)

            if som == testgevallen[t]:
                print('Output moet zijn: ', som)
                doorgaan = False

            else:
                doorgaan = True

        else:
            doorgaan = True
            vermenigvuldiging += 1

    t += 1
    doorgaan = doorgaan

I hope you guys can help me. Thanks a lot for you time in advance, I know it's quite the wall of text.

Aucun commentaire:

Enregistrer un commentaire