Reputation: 111
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?
Upvotes: 0
Views: 1050
Reputation: 2121
This is a pretty simple problem. The g
variable is not getting incremented if the if
statement does not run, so the loop will endlessly continue on the same value of g
.
The fix for this is to move the increment of g
outside of the if statement (but still in the while loop). That way it will continue past values even if they do not match the if
condition.
if records[g][1] == second_lowest_score:
names_list.append(records[g][0])
g = g + 1
Upvotes: 1