943,763 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 547
  • Python RSS
Oct 31st, 2008
0

Why Isn't This Working?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamoldest is offline Offline
9 posts
since Mar 2008
Oct 31st, 2008
0

Re: Why Isn't This Working?

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Oct 31st, 2008
0

Re: Why Isn't This Working?

Alternatively you could do this:

python Syntax (Toggle Plain Text)
  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.
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Nov 1st, 2008
0

Re: Why Isn't This Working?

I would suggest lists like this
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 3rd, 2008
0

Re: Why Isn't This Working?

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
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: equation solver
Next Thread in Python Forum Timeline: Python Class Method Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC