+ 1
Help with bucle for in this code
So, this code ask you what is the score you got in every subject. If the score > 5 l want to eliminate the subject and the score from the list and then print the subjects and scores. I can't figure out why in the bucle for, it never remove the last subject and score if it is > 5. What am l doing wrong? Thank you! subjects=['math',' english', 'fisics'] grades=[] for i in subjects: g=int(input('What is your score in '+i)) grades.append(g) c=0 for x in grades: if x>5: subjects.pop(c) grades.pop(c) c=c+1 print(subjects) print(grades)
3 odpowiedzi
+ 2
for loops works according to index no.thats why you are facing this problem
+ 1
You have written the line C=C+1 outside the for Loop ..so inside the loop c is always zero..so for any value it always removes the value of index no. 0.
I think this should be the code..
subjects=['math',' english', 'fisics']
grades=[]
for i in subjects:
g=int(input('What is your score in '+i))
grades.append(g)
c=0
for x in grades:
if x>5:
subjects.pop(c)
grades.pop(c)
c=c+1
print(subjects)
print(grades)
0
That make perfect sense and l did it, but for some reason now it is always keeping the second ([1])item of the list