gpt4 book ai didi

python - 列表索引超出范围错误 - 无法运行代码

转载 作者:行者123 更新时间:2023-12-01 04:24:47 25 4
gpt4 key购买 nike

问大家一个问题。我正在为学校编写一项作业,其中我必须模拟水在三个池塘之间流动的物理过程,以及倒入池塘 1 的污染物如何在池塘之间流动。我不断收到错误“IndexError:列表索引超出范围”。我一整天都在摆弄不同的东西,但无法让错误消失。

下面是我的代码:(仅供引用,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])

最佳答案

打错了:

def Outflow1(timeUnput):

必须是:

def Outflow1(timeInput):

关于python - 列表索引超出范围错误 - 无法运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33282505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com