[Tutor] Two subsequent for loops in one function - I got it!
Rafael Knuth
rafael.knuth at gmail.com
Sat Nov 23 20:57:54 CET 2013
@Peter
@Steven
@Don
@Danny
thank you *so much" for explaining the concept of a nested for loop!
Your simplified example Steven made it very clear to me:
for x in range(2, 7):
print("outer loop, x =", x)
for y in range(2, x):
print("inner loop, x =", x, "y =", y)
I have only one question left.
Here's my original program again:
for x in range(2, 10):
for y in range(2, x):
if x % y == 0:
print(x, "equals", y, "*", x//y)
break
else:
print(x, "is a prime number")
So the first output of the outer loop is: 2.
It's then passed to the inner loop:
for y in range(2,x):
if x % y == 0:
...
And I was wondering what is happening inside that loop.
The output of
for y in range (2,2):
should be ... none - correct?
What exactly happens on the next line of code?
if x % y == 0
To me it looks like
if 2 % "no value" == 0
is executed here which I assume causes the loop to break - correct?
Just want to understand how Python deals with "no values" within a program.
Thanks in advance!
All the best,
Raf
More information about the Tutor
mailing list