Sah*_*ory 2 python list typeerror pycharm python-3.x
numbers = [2 , 5 , 10]
for n in range[1,20]:
if n in numbers:
continue
print (n)
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
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
Run Code Online (Sandbox Code Playgroud)
寻找答案,但没有发现它是新的python所以不要生气,如果这是一个愚蠢的Qustion奇怪的事情这个代码正好在youtube的教程上使用它的工作我使用pycharm我的python应用程序有任何错误或者ide
range 它应该是一个功能 range(1,20)
这就是该行for n in range[1,20]:应该阅读
for n in range(1,20):
Run Code Online (Sandbox Code Playgroud)
正如您在文档中看到的那样
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
Run Code Online (Sandbox Code Playgroud)
您需要使用括号而不是方括号,您for n in range[1,20]:应该是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
19
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5379 次 |
| 最近记录: |