Python: "TypeError: ' Type ' object is not subscriptable"

Source: Internet
Author: User
Tags stdin

At present, StackOverflow finds solutions to two situations:

1, TypeError: ' Type ' object is not subscriptable when indexing in to a dictionary

I have the multiple files that I need to the load so I ' m using a to dict shorten things. When I run I get a "TypeError: ' Type ' object was not subscriptable" Error. How can I get the 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 the 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, and in R Eality it is not.

The type of is dict a type . All types is objects in Python. Thus you is actually trying to index into the type object. This is the error message says the "' type ' object was 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 be blindly assign dict to the name, and you really don ' t want to does 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 so you must assign variables prior to trying to use them. If you simply reorder the statements of your question, it'll 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 This 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-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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.