count: int = 0
while count < len(stocksOwned):
print(stocksOwned[count][count][0],'\nsakuma cena-',stocksOwned[count][count][1])
count += 1
stocksOwned = [[['Microsoft', 150, 0.01, 0, 0]], [['Tesla', 710, 0.0424, 0, 0]]]
Traceback: print(stocksOwned[0][count][0],'\nsakuma cena-',stocksOwned[0][count][1]) IndexError: list index out of range
我似乎不明白为什么指数超出范围。我知道索引从0开始。我在这里没有看到或理解什么
这实际上是一个列表的列表。。。以下是您打印的方式:
您的意思可能是:
stocksOwned = [['Microsoft', 150, 0.01, 0, 0], ['Tesla', 710, 0.0424, 0, 0]]
(列表列表)列表的第二个维度只有一个索引
将为您提供正确的值。拥有
stocksOwned[count][count][0]
将使它索引下一个不存在的列表这就是它的样子。所以中间的索引1会抛出错误。
您正在调用
stocksOwned[count][count]
,这将导致基于stocksOwned = [[['Microsoft', 150, 0.01, 0, 0]], [['Tesla', 710, 0.0424, 0, 0]]]
的错误。 使用以下代码:相关问题 更多 >
编程相关推荐