Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

normal list 'type' object is not subscriptable

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

I'm getting this error

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

looked for an answer but didnt find it im new to python so dont be angry if this is a stupid qustion the weird thing this code is used exactly on a tutorial on youtube and its working i use pycharm is there any thing wrong in my python app or ide

like image 763
Sahoory Avatar asked Jul 30 '26 17:07

Sahoory


1 Answers

range is a function it should be range(1,20)

That is the line for n in range[1,20]: should read

for n in range(1,20):

As you can see in the documentation

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

(Emphasis mine)

A small demo

>>> 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
like image 117
Bhargav Rao Avatar answered Aug 01 '26 06:08

Bhargav Rao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!