| | |
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 |
abrupt ansi anti approximation array assignment avogadro backend beginner binary bluetooth builtin calculator character cmd code converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary drive dynamic examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse mysqlquery number numbers output parsing path plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion scrolledtext sqlite statistics stdout string strings sum table terminal text textarea thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows write wxpython xlib






