I have a logical doubt.
What I want is to display the list with its index, but not including the zero index. Using the while loop works but with the for. I am not clear why.
'''super_list = ["",
"Opcion 1",
"Opcion 2",
"Opcion 3",
"Opcion 4",
"Opcion 6",
"Opcion 7",
"Opcion 8"]
def recorrer(cadena,posicion):
#recorrido 1
posicion=1
while posicion<len(cadena):
print(cadena.index(cadena[posicion]),cadena[posicion])
posicion += 1
#recorrido dos
print("Recorrido mediante el for")
for posicion in cadena:
print(cadena.index(cadena[posicion]),cadena[posicion])
#print(cadena.index(posicion),posicion)
'''
The correct way to traverse a list through its indices with a
for
is usingrange
to set the limit. Since you want to start at index 1, not zero, you userange(1, len(cadena))
.The code looks like this:
The correct way would be
The print(string.index(string[position]),string[position]) line was giving you problems and on the other hand, to stop printing the 0, it is suggested to validate as a condition as I placed in the if.