gpt4 book ai didi

python - 正常列表 'type' 对象不可订阅

转载 作者:行者123 更新时间:2023-11-28 21:16:04 25 4
gpt4 key购买 nike

numbers = [2 , 5 , 10]
for n in range[1,20]:
if n in numbers:
continue
print (n)

我遇到了这个错误

C:\Python34\python.exe "F:/google drive/3d projects/python/untitled/0001.py"
Traceback (most recent call last):
File "F:/google drive/3d projects/python/untitled/0001.py", line 2, in <module>
for n in range[1,20]:
TypeError: 'type' object is not subscriptable

寻找答案但没有发现它是 python 的新手所以如果这是一个愚蠢的问题请不要生气这个代码在 youtube 上的教程及其工作中使用的奇怪的事情我使用 pycharm 我的 python 应用程序或 ide 有什么问题吗

最佳答案

range 是一个函数,它应该是 range(1,20)

那是行 for n in range[1,20]: 应该读作

for n in range(1,20):

正如您在 documentation 中看到的那样

range(start, stop[, step])
This is a versatile function to create lists containing arithmetic progressions

(强调我的)

一个小演示

>>> numbers = [2 , 5 , 10]
>>> for n in range(1,20):
... if n in numbers:
... continue
... print (n)
1
3
4
6
7
8
9
11
12
13
14
15
16
17
18
19

关于python - 正常列表 'type' 对象不可订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260113/

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