Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How is my list assignment is out of range?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading