| | |
Repetitive code problem
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I'm having a problem sizing down this block of code:
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.
Python Syntax (Toggle Plain Text)
if POS == 1: POS1 = "->" else: POS1 = " " if POS == 2: POS2 = "->" else: POS2 = " " if POS == 3: POS3 = "->" else: POS3 = " " if POS == 4: POS4 = "->" else: POS4 = " " if POS == 5: POS5 = "->" else: POS5 = " " if POS == 6: POS6 = "->" else: POS6 = " " if POS == 7: POS7 = "->" else: POS7 = " " if POS == 8: POS8 = "->" else: POS8 = " " if POS == 9: POS9 = "->" else: POS9 = " " if POS == 1: POS1 = "->" else: 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.
-1
#2 Oct 11th, 2009
python Syntax (Toggle Plain Text)
locals_dict = locals() for i in range(1, 10): var_name = "POS" + str(i) if i == POS: locals_dict[var_name] = "->" else: 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.
•
•
Join Date: Oct 2007
Posts: 149
Reputation:
Solved Threads: 38
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]
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)
pos=[' ' for i in range(10)] # defines a list inited with [' ', ' ',... ten times] pos[POS-1]='->'
Last edited by jice; Oct 11th, 2009 at 4:10 pm.
0
#5 Oct 11th, 2009
Similar to jice's code:
python Syntax (Toggle Plain Text)
# simply ignore index zero pos = [' '] * 11 print( pos ) POS = 1 pos[POS] = '->' print( pos ) """my display --> [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', '->', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] """
Should you find Irony, you can keep her!
•
•
Join Date: Oct 2007
Posts: 149
Reputation:
Solved Threads: 38
0
#7 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.
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]...
![]() |
Similar Threads
- Email code problem (PHP)
- Code Problem (Modifying Book Code to adapt to program) (Java)
- login code problem (VB.NET)
- can anyone help me with this progam,and write the code for this problem? (C)
- Code problem with an array displaying list (C#)
- Problem with a snippet of code in a shell program (C)
- do while; if else? am I using the right code for the problem? (C++)
- DrawHouse Code Problem (Java)
Other Threads in the Python Forum
- Previous Thread: POST value related question
- Next Thread: How to do Input in Python?
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor class clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically event events examples external file float format frange function google gui hints homework i/o iframe import info input java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython






