[Tutor] Else Clause In A Loop
kinuthia muchane
muchanek at gmail.com
Mon May 12 22:11:42 CEST 2008
On Mon, 2008-05-12 at 14:08 -0400, "Simón A. Ruiz" wrote:
> I'll try my hand at this:
>
> The outside for loop is looking through every number up to the variable
> "number".
No quarrel here.
>
> For each of those numbers, it checks to see if any number between 2 and
> i is divisible into i. If it finds anything, we know it's not a prime,
> and so it breaks out of that second loop without completing it, which
> means the else block isn't executed.
This is where I am getting lost. When the variable 'number' is 3, it
means that in that instance the inner 'for' statement will be 'for j in
range(2,3)', hmmm which means that we will be dividing each element by 2
in the first 'for' statement and checking whether it is true , no? But
2%2 is zero, so, in my warped logic, the inner 'for' loop should break
and the else clause will not execute!
>
> If it can't find anything that i is divisible by, then that inside for
> loop finishes without breaking, we know that i is a prime number, and
> the "else" clause is executed.
This is where it gets even more interesting for me. Wont 'i' in one
instance be 8, so at that particular moment there will be a 7 in the
inner 'for' loop which will be divisible by one of our 'prime' numbers
ie 7?! I know I am wrong but for some reason I cannot see the light! :-)
>
> range(2,2) is is an empty list, so the loops ends uneventfully without
> doing anything and the else clause is executed.
>
> I'm not an expert, and this seems to me to be what's happening there.
>
> Simón
All the same, thanks...
>
> kinuthia muchane wrote:
> > Hi,
> >
> > I learnt that a loop can have an else clause. And that this clause
> > executes when the loop TERMINATES. In a while loop when the condition
> > becomes false, and in a for loop when a sequence is exhausted. When I
> > write the following code it seems to work:
> >
> > for n in [1,2,3,4,5]:
> > print 'we are in the loop',n
> > else:
> > print 'we are now EXITING the loop',n
> >
> > which results in:
> >
> > we are in the loop 1
> > we are in the loop 2
> > we are in the loop 3
> > we are in the loop 4
> > we are in the loop 5
> > we are now EXITING the loop 5
> >
> > Or:
> >
> > n = 1
> > while n <= 5:
> > print 'we are in the loop',n
> > n += 1
> > else:
> > print 'we are now EXITING the loop',n
> >
> > ...which gives:
> >
> > we are in the loop 1
> > we are in the loop 2
> > we are in the loop 3
> > we are in the loop 4
> > we are in the loop 5
> > we are now EXITING the loop 6 (it spills over here!)
> >
> > This has served to confuse me more. Would someone please kindly explain
> > how all this fits into the code below which searches (and finds!) for
> > prime numbers...
> >
> >
> > def prime():
> > number = int(raw_input("Enter a number :"))
> > for i in range(2,number):
> > for j in range(2,i):
> > if i%j == 0:
> > break
> > else:
> > print i, "is a prime number"
> > prime()
> >
> > ...especially in the instance when number is 2 in the first for
> > statement, for then we will have for j in range(2,2)! Or what is going
> > on??
> >
> > Thanks,
> > Kinuthia...
> >
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list