[Tutor] for loop

Terry Carroll carroll at tjc.com
Wed Jun 11 04:07:51 CEST 2008


On Tue, 10 Jun 2008, Sean Novak wrote:

> I know I'm going to feel stupid on this one..
> 
> I would normally write this in PHP like this:
> 
> for($i=1; i< count($someArray); $i++)
> {
> 	print $someArray[i]
> }
> 
> essentially,, I want to loop through an array skipping "someArray[0]"

Like this?

>>> someArray = ["able", "bakera", "charley", "dog", "fox"]
>>> for i in range(1,len(someArray)):
...   print somearray[i]
...
baker
charley
dog
fox
>>>

> I've tried this to no avail
> 
>   count = 0
>   for i in range(1,10):
> 	 if count == 0:
> 		continue
> 	else:
> 		count += 1
> 		print i
> 		continue
> 
> it prints absolutely nothing.

Right.

>   count = 0

count is now equal to 0

>   for i in range(1,10):

you'll loop through 9 times, from 9 up to but not including 10.

> 	if count == 0:
> 		continue

And because you initialized count to 0, it will always take this branch...


> 	else:
> 		count += 1
> 		print i
> 		continue


and never this branch where you increment count.



More information about the Tutor mailing list