I have to find the number of pupils who achieved 75% or more in three different exams. I wrote the code in two different versions where version 2 is successful but I am unable to find the correct value from version 1.
Version 1
literacy = [42,72,80,82,76]
numeracy = [55,70,75,79,48]
technology = [77,64,55,82,52]
count = 0
for counter in range(len(literacy)):
if literacy[counter] >= 75 and numeracy[counter] >= 75 and technology[counter] >= 75:
count = count 1
print(count)
output (should result 7)
1
second version
literacy = [42,72,80,82,76]
numeracy = [55,70,75,79,48]
technology = [77,64,55,82,52]
count = 0
for counter in range(len(literacy)):
if literacy[counter] >= 75:
count = count 1
if numeracy[counter] >= 75:
count = count 1
if technology[counter] >= 75:
count = count 1
print(count)
output
7
Any advise how to fix the first version.
CodePudding user response:
For the first version, It's iterated only 5 times so if you increment values by 1
you will not get the expected result 7
. So the fixing of first version is your second version itself.
Here is another attempt from me.
count = 0
for i in literacy numeracy technology:
if i >= 75:
count = count 1
In one line code,
In [1]: len([i for i in literacy numeracy technology if i >= 75])
Out[1]: 7