What's the meaning of the 3 -1 parameters in the range of rhe bucle for? Thanks! Solved! | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
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))

27th May 2020, 12:47 PM
GusPrzygora
GusPrzygora - avatar
6 Respuestas
+ 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.
27th May 2020, 1:02 PM
Russ
Russ - avatar
+ 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
27th May 2020, 1:15 PM
Russ
Russ - avatar
+ 2
Thank you very much guys. I was having difficulties popping out elements from a list, but now everything is clear.
27th May 2020, 1:36 PM
GusPrzygora
GusPrzygora - avatar
+ 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.
27th May 2020, 1:18 PM
Russ
Russ - avatar
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    
27th May 2020, 12:59 PM
GusPrzygora
GusPrzygora - avatar
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?
27th May 2020, 1:09 PM
GusPrzygora
GusPrzygora - avatar