列表索引超出范围错误 - 无法获取要运行的代码

时间:2015-10-22 13:43:07

标签: python

大家都在问你。我正在编写一份学校作业,我必须模拟三个池塘之间流动的水的物理过程,以及如何将污染物倾倒到池塘1之间。我不断收到错误" IndexError:列表索引超出范围"。我整天都在做不同的事情,但不能让错误消失。

以下是我的代码:(fyi python说我在第49,32和38行有错误。)

POND_1 = [0]
POND_2 = [0]
POND_3 = [0]

# POND 1
def pond1(timeInput):
    pollutants = Inflow3(timeInput-1)-Outflow2(timeInput-1)+POND_1[timeInput-1]
    total = leakingInput*timeInput
    if total <= maximumInput:
        pollutants += leakingInput
    return pollutants

def Inflow3(timeInput):
    return 0.005*POND_3[timeInput]

def Outflow2(timeInput):
    return 0.005*POND_1[timeInput]

#POND 2
def pond2(timeInput):
    return Inflow1(timeInput-1)-Outflow3(timeInput-1)+POND_2[timeInput-1]

def Inflow1(timeInput):
    return 0.005*POND_1[timeInput]

def Outflow3(timeInput):
    return 0.005*POND_2[timeInput]

#POND 3
def pond3(timeInput):
    return Inflow2(timeInput-1)-Outflow1(timeInput-1)+POND_3[timeInput-1]

def Inflow2(timeInput):
    return 0.005*POND_2[timeInput]

def Outflow1(timeUnput):
    return 0.005*POND_3[timeInput]

# User Input
maximumInput = int(input("Please enter the maximum amount of pollutant: "))
leakingInput = float(input("Please enter the rate in which the pollutant is leaking: "))
timeInput = int(input("Please enter the amount of time you would like the simulation to run in minutes: "))

# Amount of Pollutant in Pond every hour calculation
for n in range(0, timeInput+1):
    POND_1.append(pond1(n))
    POND_2.append(pond2(n))
    POND_3.append(pond3(n))

    if n % 60 == 0:
        print("The amount of pollutant in Pond 1 is: ", POND_1[n])
        print("The amount of pollutant in Pond 2 is: ", POND_2[n])
        print("The amount of pollutant in Pond 3 is: ", POND_3[n])

    if n == timeInput:
        print("Final number of pollutants in Pond 1", POND_1[timeInput])
        print("Final number of pollutants in Pond 2", POND_2[timeInput])
        print("Final number of pollutants in Pond 3", POND_3[timeInput])

1 个答案:

答案 0 :(得分:2)

这是一个错字:

def Outflow1(timeUnput):

必须是:

def Outflow1(timeInput):