samedi 5 mai 2018

python simple if elif mismatched result

This if-elseif is simple and straightforward. index is a 1-d array with values of 0-5 only. As you can see from the image, the only correct if-elseif is only for index[i]==0 and index[i]==1. For index[i]==5, it was supposed to print f but the result was printed as d. What went wrong? Click to view image of the output

for i in index:

    print(i)
        if index[i]==0:
          print(" :a")
        elif index[i]==1:
            print(" :b")
        elif index[i]==2:
            print(" :c")
        elif index[i]==3:
            print(" :d")
        elif index[i]==4:
            print(" :e")
        elif index[i]==5:
            print(" :f")

Aucun commentaire:

Enregistrer un commentaire