dimanche 25 avril 2021

Why is a line in my if statement running every time? (python)

I am working on a project were the user inputs a number and a list, and whatever item in the list is closest to the number, is printed out. I have come across a problem were the line in my if() statement in my while loop is running every time? I have a feeling it has something to do with indenting if() statements in python 3 but I am not certain. Anybody know why this is happening?

import math
MatchingI = math.inf

while i < len(compareList):
    if (abs(int(mainNum) - int(compareList[i])) < MatchingI):
        MatchingI = int(compareList[i])
    i += 1

Aucun commentaire:

Enregistrer un commentaire