0

I am new to python and programming in general. I have received many syntax errors in my program. most have been Index errors. When I run it now what I get is:

"Traceback (most recent call last):
  File "C:\Python33\HW3 playing around.py", line 133, in <module>
    Main()
  File "C:\Python33\HW3 playing around.py", line 32, in Main
    EmployeeNumbers()
  File "C:\Python33\HW3 playing around.py", line 69, in EmployeeNumbers
    Sal[Index] = float(input("Enter Employee salary here: "))
IndexError: list assignment index out of range"

I have no idea how to solve both this error and many others that this program has, any help would be appreciated.

-Jacob

# Description: This program will Calculate the Average, Maximum, and Minimum Salaries of employees

#Declare Variables
EmpNum = 0
SalAVG = 0
Index = 0
SalTot = 0

# Start Main
def Main():

# Get Number of employees
    EmpNum = int(input("Enter the number of employee's here: "))
    if EmpNum <=0:
            print("Please enter positive number")

    while Index < EmpNum:

        # Call EmployeeNames
        global Name
        global Index
        global SalTot
        Name = [Index]
        EmployeeNames()

        # Call EmployeeNumbers
        global Sal
        Sal = [Index]
        EmployeeNumbers()

        # Calculate SalTot
        SalTot = SalTot + Sal[Index]

        # Increase Index
        Index = Index + 1

    # Calculate and output AVG
    SalAVG = SalTot / Index
    print("The average salary is $", SalAVG)

    # Call and output Maximum
    Maximum()
    print("The highest paid employee is ", EmpName, " With a salary of $")

    # Call and output Minimum
    global Temp
    global Switch
    Minimum
    print("The Lowest paid employee is ", EmpName, " With a salary of $")

# Arrays

# EmployeeNames array
def EmployeeNames():
    # Bind global parts
    global Name
    global Index
    # Run EmployeeNames
    Name[EmpNum] = str(input("Enter employee name here: "))

# EmployeeNumbers Array
def EmployeeNumbers():
    #Bind Global parts
    global Sal
    #Run EmployeeNumbers
    Sal[Index] = float(input("Enter Employee salary here: "))
    if Sal[EmpNum] > 200000:
        print("Please enter lower salary")
        Sal[EmpNum] = float(input("Enter Employee salary here: "))
    if Sal[EmpNum] < 0:
        print("Please enter positive number")
        Sal[EmpNum] = float(input("Enter Employee salary here: "))

# Maximum array
def Maximum():
    # Bind global parts
    global Temp
    global Switch
    global Name
    Index = 1
    Temp = 0
    Switch = 1
    while Switch > 0:
        Index = 1
        if Sal[Index] > Sal[Index + 1]:
            # Call NameSwitch
            global TempName
            global Name
            NameSwitch()
            Temp = Sal[Index]
            Sal[Index] = Sal[Index + 1]
            Sal[Index + 1] = Temp
            Switch = Switch + 1
            Index = Index + 1
        Switch = 1

# Minimum array
def Minimum():
    # Bind global parts
    global Temp
    global Switch
    global Name
    Index = 1
    Temp = 0
    Switch = 1
    while Switch > 0:
        Index = 1
        if Sal[Index] < Sal[Index + 1]:
            # Call NameSwitch
            global TempName
            global Name
            NameSwitch()
            Temp = Sal[Index]
            Sal[Index] = Sal[Index + 1]
            Sal[Index + 1] = Temp
            Switch = Switch + 1
            Index = Index + 1
        Switch = 1

# NameSwitch array
def NameSwitch():
    #Bind global parts
    global TempName
    global Name
    TempName = ""
    TempName = Name[Index]
    Name[Index] = Name[Index + 1]
    Name[Index + 1] = TempName

Main()
4

2 回答 2

1

我不会修复您的代码,但您的问题可以简化为:

>>> some_list = []
>>> some_list[0] = "Hello World"
IndexError: list assignment index out of range

要修复它,您需要以初始大小开始列表:

>>> some_list = [None]
>>> some_list[0] = "Hello World"

或附加到空列表:

>>> some_list = []
>>> some_list.append("Hello World")
于 2013-04-27T22:16:16.607 回答
0

您的主要问题源于使用全局变量。不要创建全局变量,而是使用变量作为参数定义函数,如下所示:

def Maximum(Temp,Switch,Name):

然后像这样调用函数

Maximum(Temp,Switch,Name)

这样,您可以在定义函数时跟踪您的函数所需的一切。

回到你的错误,问题是函数中没有定义索引。像这样重新创建函数头:

def EmployeeNumbers(sal,index):

主要是这样称呼它:

EmployeeNumbers(sal, index)

最后,在 main 中定义所有变量,因此在调用时不需要将它们传递给 main。

于 2013-04-27T22:16:21.817 回答