• Register
1 vote
33 views

Problem:
I have multiple files that I need to load so I'm using a dict to shorten things. When I run my code it throws me back an error “ type' object is not subscriptable ”

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))

I am not sure about the error. Where is the problem in my program?

 

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes
 
Best answer

Solution:

In python generally, it throws that NameError if you define the variable.  anyway, you've managed to stumble upon a name that already exists in Python. In python, dict is a built-in type like an int. You need to keep in mind that all types are object in python. In the manner, you are trying to index into the type object that’s the reason you are getting the error.

Have a look into the codes below: 

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))

I’ve modified your program a little bit and certainly, it works fine.

For better understanding, you may check the python Doc

 

13 9 6
94,240 points

Related questions

0 votes
1 answer 16 views
16 views
Problem: 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
asked Mar 16 ummesalma 25.2k points
0 votes
1 answer 11 views
11 views
Problem: I'm writing some code to create an unsorted list but whenever I try to insert a list using the insert method I get the 'method' object is not subscriptable error. Not sure how to fix it. Thanks.
asked Mar 16 ummesalma 25.2k points
0 votes
1 answer 1 view
1 view
Problem: I am getting this error please help: Typeerror: 'zip' object is not subscriptable
asked Mar 16 ummesalma 25.2k points
0 votes
1 answer 14 views
14 views
Problem: I am facing following error: post data should be bytes, an iterable of bytes, or a file object. it cannot be of type str.
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 1.8K views
1.8K views
Problem : I am trying to convert my working Python 2.7 code into Python 3 code but I am facing following error post data should be bytes, an iterable of bytes, or a file object. it cannot be of type str.
asked Nov 16, 2019 peterlaw 6.9k points
0 votes
1 answer 8 views
8 views
Problem: after upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Mar 16 ummesalma 25.2k points
0 votes
1 answer 3 views
3 views
Problem: Please help me … how can I solve it? typeerror: 'dict_items' object does not support indexing
asked Apr 6 Ifra 24.4k points
0 votes
1 answer 2 views
2 views
Problem: Hello ... Can anyone tell me that why I got this error? dict_keys object does not support indexing
asked Apr 6 Ifra 24.4k points
0 votes
1 answer 8 views
8 views
Problem: I am getting this error: TypeError: 'set' object does not support indexing Please help
asked Mar 24 ummesalma 25.2k points
0 votes
1 answer 309 views
309 views
Problem : When I try to run following command: python manage.py updatestats I get the error as below: “TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'” I am unable to figure out ... ) File "/Users/alecxe/project/Dev/project_files/project/main/management/commands/updatestats.py", line 34, in handle unique_visits=display_unique_visits(hostname),)
asked Jan 2, 2020 alecxe 7.5k points