Strange outcome using enumerate()

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

Join Date: Jun 2009
Posts: 5
Reputation: dads is an unknown quantity at this point 
Solved Threads: 1
dads dads is offline Offline
Newbie Poster

Strange outcome using enumerate()

 
0
  #1
Oct 25th, 2009
I have to make some bookmarks so I quickly knocked up the script below and ran it. It created the files fine but with a with an incorrect ip in the second file

The second bookmark created should be named York.URL which it is but the ip address of the url is 192.65.0.95 where it should be 192.65.2.95. It uses the enumerate value and adds 1 each time.

I'd imagine I'd be able to get the desired outcome by using other methods but what is going on here? Am I missing something obvious?

  1. from __future__ import print_function
  2.  
  3. shops = [ 'Windermere',
  4. 'York',
  5. 'Chester' ]
  6.  
  7. for i, s in enumerate(shops):
  8. f = open('%s.URL' % (s), 'w')
  9. f.write('[InternetShortcut]\nURL=http://192.65.%s.95/BackO' % (i+1))
  10. f.close()
  11. print('%s completed' % s)
Last edited by dads; Oct 25th, 2009 at 2:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
Oct 25th, 2009
This little test code works fine ...
  1. shops = [ 'Windermere',
  2. 'York',
  3. 'Chester' ]
  4.  
  5. for i, s in enumerate(shops):
  6. print(i, s)
  7. print('[InternetShortcut]\nURL=http://192.65.%s.95/BackO' % (i+1))
  8.  
  9. """my result -->
  10. (0, 'Windermere')
  11. [InternetShortcut]
  12. URL=http://192.65.1.95/BackO
  13. (1, 'York')
  14. [InternetShortcut]
  15. URL=http://192.65.2.95/BackO
  16. (2, 'Chester')
  17. [InternetShortcut]
  18. URL=http://192.65.3.95/BackO
  19. """
... and so does your code (removed first line since I am using Python25).
Last edited by vegaseat; Oct 25th, 2009 at 2:50 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 963
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 221
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #3
Oct 25th, 2009
Originally Posted by vegaseat View Post
(removed first line since I am using Python25).
Why don't you install 2.6 ? It's even better.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: dads is an unknown quantity at this point 
Solved Threads: 1
dads dads is offline Offline
Newbie Poster
 
0
  #4
Oct 25th, 2009
I've just tried it again and it works. How odd, I thought I was going mad!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 127
Reputation: slate is an unknown quantity at this point 
Solved Threads: 31
slate slate is offline Offline
Junior Poster
 
0
  #5
Oct 25th, 2009
Could not reproduce it on python 2.6.2 linux either.
Maybe you have run the pyc version, which was older.
Last edited by slate; Oct 25th, 2009 at 5:16 pm.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC