• Register
1 vote

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


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 7 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 Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 1.3K 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 238 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
0 votes
1 answer 1.9K 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
0 votes
1 answer 14 views
Problem: I've install Python 3.4 and Python 3.6 on my local machine successfully, but am unable to install packages with pip3. When I execute pip3 install <package>, I get the following SSL related error: pip is configured with locations that require TLS/SSL, ... matching distribution found for <package> How can I fix my Python3.x install so that I can install packages with pip install <package>?
asked Jan 9 Mashhoodch 5.6k points
1 vote
1 answer 209 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, 2020 stewart 4k points
0 votes
1 answer 5.8K views
Problem : I am getting following error: pytesseract.pytesseract.tesseractnotfounderror: tesseract is not installed or it's not in your path
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
1 answer 4 views
Problem: I am not an experienced programmer. I am trying to run following program: value = True number = [1,2,3,4] for i in number: if i%2 == 0 : value = True else: value = False for i in range(0, len(number)): print("The sequece of values is: ", ... false. The number that completely divides by 2 should print true and the others should print false. I have no idea what I am doing wrong. Any help?
asked 3 days ago Code Learner 5.5k points
0 votes
1 answer 8 views
Problem: Hi there! I am a beginner and do not have much experience in programming. I am trying to run the following program: entries = int(input("Enter the number of items you want to add in the list: ")) lis = [] for i in range(1, entries+1): lis.append ... not know what I am doing wrong. This program was working correctly till yesterday, but I do not know what is wrong with it now. Please help.
asked 3 days ago Code Learner 5.5k points
0 votes
1 answer 17 views
On making a small program for math there returned an error "TypeError: 'NoneType' object is not subscriptable. Never before has been seen, this error, so no idea what it means. import math print("The format you should consider:") print str("value 1a")+str(" + ")+ ... /Nathan/Documents/Python/New thing", line 16, in <module> a = lista[1] - lista[0] TypeError: 'NoneType' object is not subscriptable
asked Oct 24, 2020 psandprop 2.4k points