944,154 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 642
  • Python RSS
Oct 11th, 2009
0

Repetitive code problem

Expand Post »
I'm having a problem sizing down this block of code:

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 11th, 2009
-1
Re: Repetitive code problem
python Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Oct 11th, 2009
0
Re: Repetitive code problem
Yes, I see how that would be considered a hack. Will it be fairly reliable? Or will it crash most of the time.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 11th, 2009
0
Re: Repetitive code problem
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]
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 64
Solved Threads: 56
Posting Whiz in Training
jice is offline Offline
225 posts
since Oct 2007
Oct 11th, 2009
0
Re: Repetitive code problem
Similar to jice's code:
python Syntax (Toggle Plain Text)
  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. """
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 11th, 2009
0
Re: Repetitive code problem
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.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 11th, 2009
0
Re: Repetitive code problem
Click to Expand / Collapse  Quote originally posted by AutoPython ...
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]...
Reputation Points: 64
Solved Threads: 56
Posting Whiz in Training
jice is offline Offline
225 posts
since Oct 2007
Oct 12th, 2009
0
Re: Repetitive code problem
OH, now I get it, sorry, yes it does work. Thank you!
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: POST value related question
Next Thread in Python Forum Timeline: How to do Input in Python?





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


Follow us on Twitter


© 2011 DaniWeb® LLC