The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
counter does not go forward
Discuss counter does not go forward in the Python Programming forum on Dev Shed. counter does not go forward Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 23rd, 2003, 12:35 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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.
|

July 23rd, 2003, 12:44 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Use the '#' button to insert code.
|

July 23rd, 2003, 12:54 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
|
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.
|

July 23rd, 2003, 12:58 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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.
|

July 23rd, 2003, 12:59 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
it gets stuck to 1 i meant.
|

July 23rd, 2003, 01:08 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|

July 23rd, 2003, 06:21 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
This may not be your problem, but why don't you use ? You're creating a counter variable (i) but you're not using it.
And as netytan said, post the whole code.
|

July 23rd, 2003, 09:21 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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.
|

July 24th, 2003, 08:03 AM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|