- 2

Can someone explain to me ? for index in range(len(meal)-1-,1,-1):

Python

4th Dec 2020, 11:51 AM
Low Ze Xuan
2 Respostas
+ 1
for example , meal="Burger" so the length of meal is 6. now for(index in range (len(meal)-1,1,-1): means: for (index in range (5,1,-1) [here len(meal)=6,so len(meal)-1=5] so the loop will stop when index becomes 1,means the loop will run 4 times.
4th Dec 2020, 12:10 PM
The future is now thanks to science
The future is now thanks to science - avatar
0
range is like the following: range(from, to, step) , so index will be from len(meal)-1 to 1 by -1 eg. meal = "pizza" index will be: from len("pizza")-1 #5-1 = 4 to 1 by -1 (index in (4, 3, 2))
4th Dec 2020, 12:38 PM
Sousou
Sousou - avatar