|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
counter does not go forward
Hi!
I cannot understand why the counter in the following does not work. Any ideas? Thanks in advance. temp = a list of different cases, eg: temp = [['texta', '234', '345'],['textb', '3453', '43']] result = the list of numbers found in temp eg: result = [234, 345, 3453, 43] Code:
for i in result:
d = d + 1
sum = result[d]
output.write(str(result[d]) + ' ')
s = d
while s != len(result):
s = s + len(result)/len(temp)
sum = sum + result[s]
output.write(str(result[s]) + ' ')
output.write(str(sum) + NL)
(my apologies, I don't know why the indentation is messed up) Last edited by tricia : July 23rd, 2003 at 06:32 PM. |
|
#2
|
|||
|
|||
|
Use the '#' button to insert code.
|
|
#3
|
||||
|
||||
|
The way I see it, you did not give any initial value to d. So Python is saying "Yeah, I'm adding 1 to WHAT ?". Python does not even know what type d is if you don't initialize it. Simply add d = 0 at the beginning of your code.
|
|
#4
|
|||
|
|||
|
my mistake, i forgot to add it. I do have d=0 just before 'for i in result:' but it still won't do it. d just gets stuck to 0.
|
|
#5
|
|||
|
|||
|
it gets stuck to 1 i meant.
|
|
#6
|
||||
|
||||
|
Is this a counter script for a web page, as in CGI? Whats it surposed to do? Also this can't be the whole script, can you post the rest please?
Take care, Mark. |
|
#7
|
||||
|
||||
|
This may not be your problem, but why don't you use
Code:
for d in result And as netytan said, post the whole code. |
|
#8
|
|||
|
|||
|
Hi tricia,
It seems that you're having a bit of confusion in 'nested' loops (probably) I presume python nested loops works as any other programming language. eg: first the outer loop executed once, then executes the inner until it finishes or finds a condition, the again the outer loop again.... isn't it right? As I can see from your code you've got a 'for' loop which you you want the var 'd' to increment. Then you're using a 'while' loop. and you've alredy told us that 'd' is being initialise somewhere in your code. OK... let's see now, I think there's something wrong in the while 'loop' condition.... This is what your loop code does: 1. Goes to 'for' then blah blah 2. Goes in the 'while s != len(result):' presumably: s = d which is 1 and len(result) = 4 or 5 or 6. Which in this case you need it to be 1 != 4, and the condition is correct! Then s = s + len(result)/len(temp) s = 1 + 4/2 equals 3 then while loop check condition 3 != 4 - nop s = 3 + 4/2 equals 5 then while loop check condition 5 != 4 - nop s = 5 + 4/2 equals 7 then while loop check condition 7 != 4 - nop ... and this goes forever .... When you get in 'while' loop the program stay in it, like forever.... how do you get out of there? Code:
while s != len(result):
s = s + len(result)/len(temp)
sum = sum + result[s]
output.write(str(result[s]) + ' ')
output.write(str(sum) + NL)
A solution: If you can find a way to get out from the while loop. 'd' might be able to increment. I don't know if I'm right, anyway good luck. Mina Saiiiii! Last edited by CottonBuds : July 23rd, 2003 at 09:34 PM. |
|
#9
|
|||
|
|||
|
here is the full code, hope it helps.
Code:
text = [['txt1-0', '123', '231241', '23'],['txt1-1', '1233','23','413'],['txt2-0','34','3','5']]
def add(text):
temp = []
## read the file name, if the next filename has the same main digit, continue till you find sth different
## if not, go back and add the numbers as if in columns, and create a new entry.
## eg. text = [['txt1', '1356', '231264', '436],['txt2','34','3','5']]
a = -1; first = a + 1
for i in text:
a = a + 1
try:
if text[a+1][0][3] == text[first][0][3]:
temp.append(i)
else:
b = -1; result = []
for i in temp: ## gather all the scores in one list
b = b + 1; c = -1
for s in i[1:]:
c = c + 1
result = result + [float(temp[b][1:][c])]
d = -1; sum = 0
for i in result:
d = d + 1
sum = result[d]
s = d
while s != len(result):
s = s + len(result)/len(temp)
sum = sum + result[s]
except IndexError: pass
there should be more but i haven't written it yet. Last edited by tricia : July 24th, 2003 at 08:08 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > counter does not go forward |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|