[Tutor] IndexError and appending to lists [Was: Re: Need Helpon Assignment]
Alan G
alan.gauld at freenet.co.uk
Thu Aug 25 08:31:21 CEST 2005
> "for" loop as you suggest, the program won't enter the loop unless
> "s" is initialized so that it's in "input". How do I do that?
for s in input:
means that s takes on each value in input.
input is your file. Thus s takes on the value of each line in
the input file. You don't need to initialise s before entering
the loop as you would with a while loop. Similarly you don't
need to test for the end of the file, 'for' does all that too.
Take a look at the 'Loops' topic and then the 'Handling Files' topic
in my tutorial for more info on this.
> Also, near the end of your remarks you say that the code at the
> bottom of my program doesn't do anything. It does for me.
Lets take a look:
>>> print N
>>> for i in range(N):
>>> T[i] <-------- This does nothing
>>> print T[i]
The line that simply has the value in it will not do anything.
It will not print out the value, you need to call print for that
to happen.
> put in those print statements to verify that the contents of those
> variables are what I expected them to be. That's all that mess is
> for.
The debug/test print statements are fine, I was only pointing out
that one line did nothing, not the entire block.
HTH,
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list