0
What's the meaning of the 3 -1 parameters in the range of rhe bucle for? Thanks! Solved!
subjects = ["Matemáticas", "Física", "Química", "Historia", "Lengua"] for i in range(len(subjects)-1, -1, -1): score = float(input("¿Qué nota has sacado en " + subjects[i] + "?")) if score >= 5: subjects.pop(i) print("Tienes que repetir " + str(subjects))
6 Antworten
+ 2
range() takes (up to) 3 arguments. The first is the starting number, then second is the ending number (though the end number is not included) and the third is the step.
In your code, the starting number is len(subjects)-1, which means that it will start counting from the index of the last element of subjects, then the second argument is -1 which means it will count down to 0 (because -1 isn't included) and it will count down -1 at a time. All these are needed to ensure it counts down correctly.
+ 2
GusPrzygora Let me give you some examples.
range(5) -> 0,1,2,3,4
range(2,7) -> 2,3,4,5,6
range(3,13,3) -> 3,6,9,12
range(7,2,-1) -> 7,6,5,4,3
+ 2
Thank you very much guys. I was having difficulties popping out elements from a list, but now everything is clear.
+ 1
The reason the subject list is being iterated in reverse is because each subject is being removed from the list as it is being iterated. Doing this while iterating in the normal way will result in only every other subject being removed.
0
for i in range(len(subjects)-1, -1, -1):
In the line above, there are three -1 after len(subjects) that l am not sure what they do
0
So, would it be the same doing the iteration without the arguments? I mean, If l do the same but from 0 to the last number, wouldn't be the same result?