我的列表分配是如何超出范围的



我是python的新手,正在尝试理解2D数组。我正在尝试编写一个代码,其中有人输入并存储10名员工的员工ID、部门和工资。然后使用2D阵列存储。将输出以下内容:

  • 所有具有适当列标题的信息
  • 每个部门有多少员工在工作。将其与部门名称一起输出
  • 将部门输入为帐户、管理员和销售

这是我的代码:

EmpID = ""
Departement = ""
Salary = ""
EmpDetails = [[0 for c in range(3)] for r in range(4)]
for r in range(4):
EmpDetails[r][1]=(input("Enter the Employee ID: "))
EmpDetails[r][2]=(input("Enter the Department: "))
EmpDetails[r][3]=int(input("Enter the Salary: "))
print("Employee ID     Department     Salary")
for r in range(4):
for c in range(3):
print(EmpDetails[r][c], end = " ")
print() #??
Account=0
Adcount=0
Scount=0
for r in range(4):
if EmpDetails[r][2] == "Accounts":
Accounts=Account+1
elif EmpDetails[r][2] == "Admin":
Adcount=Adcount+1
else:
Scount=Scount+1
print("Account: ", Account, "Employees")
print("Admin: ", Adcount, "Employees")
print("Sales: ", Scount, "Employees")

当我运行模块时,错误出现在:

Enter the Employee ID: 1
Enter the Department: Accounts
Enter the Salary: 1000
Traceback (most recent call last):
File "/Users/Documents/2darray.py", line 8, in <module>
EmpDetails[r][3]=int(input("Enter the Salary: "))
IndexError: list assignment index out of range

我很困惑,我的列表分配是如何超出范围的?我过不去了,所以不能继续了。提前谢谢!

Python的索引为零。输入EmpDetails时,请改用索引0、1、2。

最新更新