简体   繁体   English

为什么我得到这个python语法indexerror

[英]why am I getting this python syntax indexerror

I am new to python and programming in general. 我是python和一般编程的新手。 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()

I'm not going to fix your code, but your problem can be simplified to: 我不会修复您的代码,但是您的问题可以简化为:

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

To fix it, you need to either start the list with an initial size: 要修复它,您需要以初始大小开始列表:

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

Or append to the empty list: 或追加到空列表:

>>> some_list = []
>>> some_list.append("Hello World")

Your major problem stems from the use of global variables. 您的主要问题源于使用全局变量。 Instead of creating global variables, define your function with the variables as arguments like this: 而不是创建全局变量,而是将变量定义为函数,如下所示:

def Maximum(Temp,Switch,Name):

Then call the function like this 然后像这样调用函数

Maximum(Temp,Switch,Name)

That way you can keep track of everything your function will need when defining it. 这样,您可以跟踪定义函数时所需的一切。

Back to your error, the problem is that Index is not defined in the function. 回到您的错误,问题在于该函数中未定义Index。 recreate the function header like so: 重新创建函数标头,如下所示:

def EmployeeNumbers(sal,index):

and in main, call it like this: 总的来说,这样称呼它:

EmployeeNumbers(sal, index)

Last, define all of your variables inside main, so you do not need to pass them into main when you call it. 最后,在main内部定义所有变量,因此在调用它时不需要将它们传递给main。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM