![](/img/trans.png)
[英]Can anyone help me to understand why i have the error “list index out of range”?
[英]i have “list index out of range” in python, but i dont understand why
datos = {"nombre": "Juan","apellido1": "Perez",
"apellido2": "Palma","sexo": "M","codigo":"30","ingreso": "2009"}
def generar_rol(datos):
from definiciones import digito_verificador
archivo= open('alumnos.dat')
zzz=0
nombret=datos['nombre']+datos['apellido1']+datos['apellido2']
for linea in archivo:
zzz+=1
todo=linea.strip().split()
print todo[2].split('|') #<----HERE (line 24)
nombre = todo[2].split('|')
nombretotal=''.join(nombre)
if nombret==nombretotal:
break
zzz= str(zzz)
Z = ["0", "0", "0"]
if len(zzz) >= 1 :
Z[-1] = zzz[-1]
if len(zzz) >= 2 :
Z[-2] = zzz[-2]
if len(zzz) >= 3 :
Z[-3] = zzz[-3]
zzz = "".join(Z)
anno=''
for i in datos:
if int(datos['ingreso'])<2010:
anno=datos['ingreso'][0]+datos['ingreso'][-1]
if int(datos['ingreso'])>=2010:
anno=datos['ingreso']
rol=anno+datos['codigo']+str(zzz)
rol= rol+'-'+digito_verificador(rol)
archivo.close()
return rol
generar_rol(datos)
我有這個錯誤...
['JUAN', 'PEREZ', 'PALMA']
Traceback (most recent call last):
File "C:\Users\pinky\Downloads\TAREA 3\TAREA 3\Tarea 3\funciones.py", line 50, in <module>
generar_rol(datos)
File "C:\Users\pinky\Downloads\TAREA 3\TAREA 3\Tarea 3\funciones.py", line 24, in generar_rol
print todo[2].split('|') #<----HERE
IndexError: list index out of range
索引超出范圍意味着您正在嘗試訪問列表中不存在的元素。 索引計數從0開始,所以您可能要用todo [1]來獲取第二個元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.