小编rsc*_*rth的帖子

Python for循环返回IndexError:列表索引超出范围

我刚刚开始学习python.我试图找到数字列表中的最小整数.相反,我得到这个错误:

Traceback (most recent call last):
  File "H:/Python/untitled/08_python.py", line 32, in <module>
    if digits[e] < temp:
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

我意识到你可以使用min(数字),但我认为到目前为止我会挑战我的知识.我很可能犯了一个简单的错误,并没有正确诊断它.如果我在列表中抛出0,一切正常.

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

low = 0
temp = 0
for e in digits:
    if digits[e] < temp:
        low = digits[e]
    temp = digits[e]

print(low)
Run Code Online (Sandbox Code Playgroud)

python for-loop list

1
推荐指数
1
解决办法
258
查看次数

标签 统计

for-loop ×1

list ×1

python ×1