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中有什么问题?
发布于 2015-03-25 23:39:19
range是一个函数,它应该是range(1,20)
这就是for n in range[1,20]:应该读取的代码行
for n in range(1,20):正如您在documentation中所看到的
range(start, stop[, step])
这是一个通用的函数,用于创建包含算术级数的列表
(强调我的)
一个小演示
>>> 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发布于 2015-03-25 23:39:16
你需要使用这些括号而不是方括号,你应该这样做: this be for n in range(1,20):
In [106]:
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
19https://stackoverflow.com/questions/29260113
复制相似问题