| | |
Why Isn't This Working?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 9
Reputation:
Solved Threads: 0
Okay heres my code...
So basically I have a bunch of images called "E1, E2, E3, E4, etc..." and I want to load them all into the cache without having to type the code for every single image....
But why doesn't this bit of code seem to work? I know it has to do something with the str() code but idk what :-(
Heres how I would normally load one of these images....
The error says "Can't assign to operator".....
Any Help?!? Thanks
for swipeload in range (0, 15, 1):
"E"+str(swipeload)+"_Path" = os.path.join("data","E"+str(swipeload)+".gif")
"E"+str(swipeload) = pygame.image.load("E"+str(swipeload)+"_Path")So basically I have a bunch of images called "E1, E2, E3, E4, etc..." and I want to load them all into the cache without having to type the code for every single image....
But why doesn't this bit of code seem to work? I know it has to do something with the str() code but idk what :-(
Heres how I would normally load one of these images....
E1_Path = os.path.join("data", "E1.gif")
E1 = pygame.image.load(E1_Path)The error says "Can't assign to operator".....
Any Help?!? Thanks
Yeah you cant assign values to a string. Thats what your program is trying to do. Well i would advise using a dictionary for this one:
This now has a dictionary where the string values are the keys and the values are what you want. The first dictionary holds the path while the second dictionary holds the image.
Hope that helps
python Syntax (Toggle Plain Text)
d1 = {} d2 = {} for swipeload in range (0, 15, 1): d1["E"+str(swipeload)+"_Path"] = os.path.join("data","E"+str(swipeload)+".gif") d2["E"+str(swipeload)] = pygame.image.load("E"+str(swipeload)+"_Path")
Hope that helps
Last edited by Paul Thompson; Oct 31st, 2008 at 5:43 pm.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Oct 2008
Posts: 45
Reputation:
Solved Threads: 6
Alternatively you could do this:
But dictionaries are a better option.
python Syntax (Toggle Plain Text)
for swipeload in range (0, 15, 1): exec('E'+str(swipeload)+'_Path = os.path.join("data","E"+str(swipeload)+".gif")') exec('E'+str(swipeload)+' = pygame.image.load("E"+str(swipeload)+"_Path")')
But dictionaries are a better option.
Last edited by tyincali; Oct 31st, 2008 at 6:47 pm.
I wish I had something cool to put here...
I would suggest lists like this
Then you can use
python Syntax (Toggle Plain Text)
cnt_img = 15 E_Path = [os.path.join("data", "E%d.gif" % k) for k in xrange(cnt_img)] E = [pygame.image.load(E_Path[k]) for k in xrange(cnt_img)]
E[k] and E_Path[k] to get the k-th image or path. mabey you could re-write it like this:
[code=python]
cnt_img = 15
E_Path = [os.path.join("data", "E%d.gif" % k) for k in xrange(cnt_img)]
E = [pygame.image.load(E_Path[k]) for k in xrange(cnt_img)]
cnt_img = 15 E_Path = [os.path.join("data", "E%d.gif" % k) for k in xrange(cnt_img)] = [pygame.image.load(E_Path[k]) for k in xrange(cnt_img)]
Now use E[k] and E_Path[k] to get the k-th image
[code=python]
cnt_img = 15
E_Path = [os.path.join("data", "E%d.gif" % k) for k in xrange(cnt_img)]
E = [pygame.image.load(E_Path[k]) for k in xrange(cnt_img)]
cnt_img = 15 E_Path = [os.path.join("data", "E%d.gif" % k) for k in xrange(cnt_img)] = [pygame.image.load(E_Path[k]) for k in xrange(cnt_img)]
Now use E[k] and E_Path[k] to get the k-th image
Last edited by tomtetlaw; Nov 3rd, 2008 at 7:38 pm. Reason: i made a mistake
...
![]() |
Similar Threads
- Flash Player stop working since Ad-aware (Web Browsers)
- Internet Explorer not working. (Web Browsers)
- Flash Player stop working and won't reinstall (Windows NT / 2000 / XP)
- Acer laptop keys not working (Troubleshooting Dead Machines)
- messanger not working. (Web Browsers)
- cd burner not working (Storage)
- Working on new design (DaniWeb Community Feedback)
Other Threads in the Python Forum
- Previous Thread: equation solver
- Next Thread: Python Class Method Help
Views: 442 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
alarm aliased application beginner calculator casino character code command cursor cx-freeze definedlines development dictionary dynamic error event examples excel exe file filename float format ftp function google graphics gui homework ideas import input java launcher line linux list lists logging loop matching microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook port prime program programming projects py2exe pygame pygtk pyqt pysimplewizard python random recursion recursive refresh return reverse scrolledtext shebang simple skinning sprite ssh statistics string strings table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip windows wxpython





