• Register
1 vote
19 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

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 3 views
3 views
from newsapi.sources import Sources import json api_key ='*******************' s = Sources(API_KEY=api_key) they input the category of news they want wanted = input('> ') source_list = s.get(category=wanted, language='en') index = 0 sources = [] getting ... 1 getting error 'type' object is not subscriptable on the line a = Article[url_] have researched but still do not understand why in my case.
asked 1 day ago psandprop 1.9k points
0 votes
1 answer 1.1K views
1.1K 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 199 views
199 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 alecxe 7.5k points
0 votes
1 answer 1.5K views
1.5K views
Problem : I am facing the issue in passing a function as a parameter to another function as follows typeerror: int() argument must be a string, a bytes-like object or a number, not 'nonetype'
asked Nov 18, 2019 peterlaw 6.9k points
1 vote
1 answer 105 views
105 views
Problem : I am beginner to Python. Currently if I try to use the request module to have the urls text then I often face below Error: Traceback (most recent call last):  File "/usr/local/lib/python3.6/site-packages/requests-2.18.1-py3.5.egg/requests/adapters.py" ... to get the url's text to parse it. Please find below my line of code for your reference: mytext = requests.get(detail_path).mytext
asked May 2 stewart 4k points