Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我做错了什么?“错误”;“索引超出范围”;试图用列表填充列表_Python - Fatal编程技术网

Python 我做错了什么?“错误”;“索引超出范围”;试图用列表填充列表

Python 我做错了什么?“错误”;“索引超出范围”;试图用列表填充列表,python,Python,我想做一个列表,里面有列表,有for,索引超出范围 我尝试了EmplbeadOS.append(),但它不起作用 def main(): 雇员人数=[] 对于范围(1)中的i: emplbeados[i][0](输入(“入口名称:)) Emplbeados[i][1](输入(“Ingree el-Apellido:”) emplbeados[i][2](int(输入(“Ingree el-Sueldo Base:”)) emplbeados[i][3](int(输入(“Ingree el-AFP

我想做一个列表,里面有列表,有for,索引超出范围

我尝试了EmplbeadOS.append(),但它不起作用

def main():
雇员人数=[]
对于范围(1)中的i:
emplbeados[i][0](输入(“入口名称:))
Emplbeados[i][1](输入(“Ingree el-Apellido:”)
emplbeados[i][2](int(输入(“Ingree el-Sueldo Base:”))
emplbeados[i][3](int(输入(“Ingree el-AFP 1 o 2:))
emplbeados[i][4](日期时间(输入(“Ingree la Fecha de Ingreo(pulsa intro cada vez 2000 12 31):”),输入(“/”),输入(“/”))
雇员人数[i][5](国际(输入:))
欢迎来到SO

empleados[0]
中没有可插入新值的列表。我发现这样的东西更容易阅读:

def main():
    empleados=[]
    for i in range(1):
        empleado_nueva = []
        empleado_nueva.append(input("Ingrese el Nombre: "))
        empleado_nueva.append(input("Ingrese el Apellido: "))
        empleado_nueva.append(int(input("Ingrese el Sueldo Base: ")))
        empleado_nueva.append(int(input("Ingrese el AFP 1 o 2: ")))
        empleado_nueva.append(datetime(int(input("Ingrese la Fecha de Ingreso(pulsa intro cada vez 2000 12 31): ")),int(input("/")),int(input("/"))))
        empleado_nueva.append(int(input("Ingrese la cantidad de hijos que tiene: ")))
        empleados.append(empleado_nueva)
    return empleados
值得一提的是,您正在尝试的索引访问模式(
empleados[i][0]=…
)只有在该索引中已经存在某些内容时才有效,例如:

>>> x = []
>>> x[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

>>> x = ['a', 'b', 'c']
>>> x[0] = 'd'
>>> x
['d', 'b', 'c']
>x=[]
>>>x[0]=1
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
索引器:列表分配索引超出范围
>>>x=['a','b','c']
>>>x[0]=“d”
>>>x
['d','b','c']

因此,append可能是最好的方法。

问题是,您正在尝试使用empleados[i]作为一个列表,其中包含一个可以插入的现有索引,而目前还没有

您需要将变量设置为一个单独的列表,然后追加它们。例如

def main():
    empleados=[]
    vars = [
        input("Ingrese el Nombre: "),
        input("Ingrese el Apellido: "),
        int(input("Ingrese el Sueldo Base: ")),
        int(input("Ingrese el AFP 1 o 2: ")),
        datetime(int(input("Ingrese la Fecha de Ingreso(pulsa intro cada vez 2000 12 31): ")),int(input("/")),int(input("/"))),
        int(input("Ingrese la cantidad de hijos que tiene: ")
    empleados.append(vars)

“我试过empleados.append(),但它不起作用”-您应该再试一次,因为这是将一个新项目附加到列表的唯一方法Initialise
empleados=[[]]
首先,然后您可以执行
empleados[0]。附加(输入(…)
,频率随您的需要而定。Hm。您在哪里
返回employeados
?我不明白