I’m trying to work out what the logical fault between these two algorithms is and I can’t seem to crack it, does anyone have an idea? Both the programs execute and get the correct answer but I can’t work out what the logical error is.
data = [["Jacob", "91"], ["John", "81"], ["James", "71"], ["Joe", "61"]]
name = "Joe"
not_found = True
index = 0
marks = 0
# algorithm A
while not_found:
if data[index][0] == name:
marks = data[index][1]
not_found = False
index = index + 1
print(marks)
# algorithm B
for i in range(len(data)):
if name == data[i][0]:
marks = data[i][1]
print(marks)
>Solution :
Since you’re looking for "logical" errors in the algorithm, algorithm A has one.
while not_found:
If the name doesn’t exist in the array, you’ll get an index error. It should be
while not_found and index < len(data):