mercredi 10 mars 2021

How to append values in a while loop by using an if statement

records = [["chi", 20.0],["beta", 50.0],["alpha", 50.0]]
a = len(records)
i = 0
b = []
while i < a:
    print(records[i][1])
    b.append(records[i][1])
    i = i + 1
print(b)
c = len(b)
#from previous question
unique_list = []
for el in b:
   if el not in unique_list:
       unique_list.append(el)
   else:
       print ("Element already in the list")
print(unique_list)
second_lowest_score = unique_list[1]
print(second_lowest_score)
names_list = []
g = 0
while g < a:
    if records[g][1] == second_lowest_score:
        names_list.append(records[g][0])
        g = g + 1
print(names_list)

What I want to do is to append the names of records which have the second lowest score (50) to the names_list. However, the while loop gives me no result. There is no syntax error so I am not sure why my code is wrong. However, when I use the previous while loop for appending the numbers, it seems to work fine. Is it not possible to use an if statement in a while loop?

Aucun commentaire:

Enregistrer un commentaire