Why Isn't This Working?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 9
Reputation: iamoldest is an unknown quantity at this point 
Solved Threads: 0
iamoldest iamoldest is offline Offline
Newbie Poster

Why Isn't This Working?

 
0
  #1
Oct 31st, 2008
Okay heres my code...

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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 950
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 147
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Why Isn't This Working?

 
0
  #2
Oct 31st, 2008
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:
  1. d1 = {}
  2. d2 = {}
  3. for swipeload in range (0, 15, 1):
  4. d1["E"+str(swipeload)+"_Path"] = os.path.join("data","E"+str(swipeload)+".gif")
  5. d2["E"+str(swipeload)] = pygame.image.load("E"+str(swipeload)+"_Path")
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
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: tyincali is an unknown quantity at this point 
Solved Threads: 6
tyincali tyincali is offline Offline
Light Poster

Re: Why Isn't This Working?

 
0
  #3
Oct 31st, 2008
Alternatively you could do this:

  1. for swipeload in range (0, 15, 1):
  2. exec('E'+str(swipeload)+'_Path = os.path.join("data","E"+str(swipeload)+".gif")')
  3. 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...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Why Isn't This Working?

 
0
  #4
Nov 1st, 2008
I would suggest lists like this
  1. cnt_img = 15
  2. E_Path = [os.path.join("data", "E%d.gif" % k) for k in xrange(cnt_img)]
  3. E = [pygame.image.load(E_Path[k]) for k in xrange(cnt_img)]
Then you can use E[k] and E_Path[k] to get the k-th image or path.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Why Isn't This Working?

 
0
  #5
Nov 3rd, 2008
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
Last edited by tomtetlaw; Nov 3rd, 2008 at 7:38 pm. Reason: i made a mistake
...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 442 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC