How is my list assignment is out of range?

Question:

I’m new to python and trying to understand 2D arrays. I’m trying to write a code in which someone inputs and stores the employee ID, department, and salary for 10 employees. Which is then stored using a 2D array The following will be outputted:

  • All the information with the appropriate column title
  • How many employees are working in each department Output it with the
    department name
  • Input the department as accounts, admin and sales

This is my code:

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")

When I’m running the module, and error comes out at:

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

I’m so confused as to how my list assignment is out of range? I can’t get past this so can’t continue on. Thank you in advance!

Asked By: AHM Mamun

||

Answers:

Python is zero-indexed. When inputting EmpDetails, use indexes 0, 1, 2 instead.

Answered By: Marcus
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.