Repetitive code problem

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

Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Repetitive code problem

 
0
  #1
Oct 11th, 2009
I'm having a problem sizing down this block of code:

  1. if POS == 1:
  2. POS1 = "->"
  3. else:
  4. POS1 = " "
  5.  
  6. if POS == 2:
  7. POS2 = "->"
  8. else:
  9. POS2 = " "
  10.  
  11. if POS == 3:
  12. POS3 = "->"
  13. else:
  14. POS3 = " "
  15.  
  16. if POS == 4:
  17. POS4 = "->"
  18. else:
  19. POS4 = " "
  20.  
  21. if POS == 5:
  22. POS5 = "->"
  23. else:
  24. POS5 = " "
  25.  
  26. if POS == 6:
  27. POS6 = "->"
  28. else:
  29. POS6 = " "
  30.  
  31. if POS == 7:
  32. POS7 = "->"
  33. else:
  34. POS7 = " "
  35.  
  36. if POS == 8:
  37. POS8 = "->"
  38. else:
  39. POS8 = " "
  40.  
  41. if POS == 9:
  42. POS9 = "->"
  43. else:
  44. POS9 = " "
  45.  
  46. if POS == 1:
  47. POS1 = "->"
  48. else:
  49. POS1 = " "

The reason why I can't figure it out is mainly because the sequence I would use a for loop with is in a variable. Any suggestions would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso
 
-1
  #2
Oct 11th, 2009
  1. locals_dict = locals()
  2. for i in range(1, 10):
  3. var_name = "POS" + str(i)
  4. if i == POS:
  5. locals_dict[var_name] = "->"
  6. else:
  7. locals_dict[var_name] = " "

discalimer: That code uses hacks and the purists may have your head for it.
Last edited by scru; Oct 11th, 2009 at 8:47 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #3
Oct 11th, 2009
Yes, I see how that would be considered a hack. Will it be fairly reliable? Or will it crash most of the time.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 149
Reputation: jice is on a distinguished road 
Solved Threads: 38
jice jice is offline Offline
Junior Poster
 
0
  #4
Oct 11th, 2009
Why do you want to have vars named POSn.
I wouldn't do that...
Very complicated. It's far easier to have one list variable where you assign pos[n]
  1. pos=[' ' for i in range(10)] # defines a list inited with [' ', ' ',... ten times]
  2. pos[POS-1]='->'
Last edited by jice; Oct 11th, 2009 at 4:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
Oct 11th, 2009
Similar to jice's code:
  1. # simply ignore index zero
  2. pos = [' '] * 11
  3. print( pos )
  4.  
  5. POS = 1
  6.  
  7. pos[POS] = '->'
  8. print( pos )
  9.  
  10. """my display -->
  11. [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  12. [' ', '->', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  13. """
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #6
Oct 11th, 2009
If I knew how the 2nd line of Jiccess code I could make it work. But I forgot to mention that each POS variable is in a certain place. I won't work to only have 1 variable. I also forgot to mention that scru's code didn't work either, it said that POS1 wasn't defined.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 149
Reputation: jice is on a distinguished road 
Solved Threads: 38
jice jice is offline Offline
Junior Poster
 
0
  #7
Oct 11th, 2009
Originally Posted by AutoPython View Post
If I knew how the 2nd line of Jiccess code I could make it work. But I forgot to mention that each POS variable is in a certain place. I won't work to only have 1 variable. I also forgot to mention that scru's code didn't work either, it said that POS1 wasn't defined.
Sorry but I don't understand what you mean.
What do you mean with "each POS variable is in a certain place" ?

If I use one variable, it contains the tenth values.
Instead of using POS1, you'll use pos[0], POS5 will become pos[4]...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #8
Oct 12th, 2009
OH, now I get it, sorry, yes it does work. Thank you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC