python: "TypeError: 'type' object is not subscriptable"

來源:互聯網
上載者:User

標籤:library   man   lin   win   解決   doc   mos   indexing   mes   

目前stackoverflow找到兩種情況的解決辦法:

1、TypeError: ‘type‘ object is not subscriptable when indexing in to a dictionary

I have multiple files that I need to load so I‘m using a dict to shorten things. When I run I get a "TypeError: ‘type‘ object is not subscriptable" Error. How can I get this to work?

m1 = pygame.image.load(dict[1])m2 = pygame.image.load(dict[2])m3 = pygame.image.load(dict[3])dict = {1: "walk1.png", 2: "walk2.png", 3: "walk3.png"}playerxy = (375,130)window.blit(m1, (playerxy))

Normally Python throws NameError if the variable is not defined:

>>> d[0]Traceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name ‘d‘ is not defined

However, you‘ve managed to stumble upon a name that already exists in Python.

Because dict is the name of a built-in type in Python you are seeing what appears to be a strange error message, but in reality it is not.

The type of dict is a type. All types are objects in Python. Thus you are actually trying to index into the type object. This is why the error message says that the "‘type‘ object is not subscriptable."

>>> type(dict)<type ‘type‘>>>> dict[0]Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: ‘type‘ object is not subscriptable

Note that you can blindly assign to the dict name, but you really don‘t want to do that. It‘s just going to cause you problems later.

>>> dict = {1:‘a‘}>>> type(dict)<class ‘dict‘>>>> dict[1]‘a‘

The true source of the problem is that you must assign variables prior to trying to use them. If you simply reorder the statements of your question, it will almost certainly work:

d = {1: "walk1.png", 2: "walk2.png", 3: "walk3.png"}m1 = pygame.image.load(d[1])m2 = pygame.image.load(d[2])m3 = pygame.image.load(d[3])playerxy = (375,130)window.blit(m1, (playerxy))


2、TypeError: ‘int‘ object is not subscriptable

I‘m trying to create a simple program that tells you your lucky number according to numerology. I keep on getting this error:

File "number.py", line 12, in <module>    sumln = (int(sumall[0])+int(sumall[1]))TypeError: ‘int‘ object is not subscriptable

My script is:

birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")summ = (int(birthday[0])+int(birthday[1]))sumd = (int(birthday[3])+int(birthday[4]))sumy= (int(birthday[6])+int(birthday[7])+int(birthday[8])+int(birthday[9]))sumall = summ + sumd + sumyprint "The sum of your numbers is", sumallsumln = (int(sumall[0])+int(sumall[1]))print "Your lucky number is", sumln`   

If you want to sum the digit of a number, one way to do it is using sum() + a generator expression:

sum(int(i) for i in str(155))

I modified a little your code using sum(), maybe you want to take a look at it:

birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")summ = sum(int(i) for i in birthday[0:2])sumd = sum(int(i) for i in birthday[3:5])sumy = sum(int(i) for i in birthday[6:10])sumall = summ + sumd + sumyprint "The sum of your numbers is", sumallsumln = sum(int(c) for c in str(sumall)))print "Your lucky number is", sumln

python: "TypeError: 'type' object is not subscriptable"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.