470,614 Members | 1,505 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

can anyone advise me

why the output of this code :
x = 0
while x < 10:
z = 0
print x
x = x + 1
while z < x:
print z,
z = z + 1

is

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 < ---extra
instead of :
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

thanks

Apr 27 '06 #1
6 864
On 27 Apr 2006 02:48:46 -0700,
mi*******@hotmail.com wrote:
why the output of this code :
x = 0
while x < 10:
z = 0
print x
x = x + 1
while z < x:
print z,
z = z + 1 is 0
Okay, that was x, from the print statement inside the x-loop.
0 1
And that's z, from the print statement inside the z-loop. z is 0, and
then when z is 1, it's not less than x, so we're done printing z's.

And then we get the next x.
0 1 2


And two more z's, 0 and 1, since x is now 2.

And another x.

Since this might be homework, I'll stop at a hint: you need to think
about when you want each printed line to end, and make sure that you
tell python to end it there.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
Apr 27 '06 #2
mi*******@hotmail.com wrote:
why the output of this code :
x = 0
while x < 10:
z = 0
print x
x = x + 1
while z < x:
print z,
z = z + 1

is

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 < ---extra
instead of :
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

thanks


Hint: You can modify your code a bit to see where the offending extra line
comes from:

x = 0
while x < 10:
z = 0
print "x" + str(x)
x = x + 1
while z < x:
print "z" + str(z),
z = z + 1

Surprised? Replace the statement responsible for the extra number with a
bare print, perhaps moving it around somewhat to avoid the extra blank
line -- and when it works rewrite the script with for loops and range() :-)

Peter
Apr 27 '06 #3
mi*******@hotmail.com wrote:
why the output of this code :
x = 0
while x < 10:
z = 0
print x
x = x + 1
while z < x:
print z,
z = z + 1

is

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 < ---extra
instead of :
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9


Does the following help?

x = 0
while x < 10:
print 'x'
x = x + 1
z = 0
while z < x:
print z,
z = z + 1

x
0 x
0 1 x
0 1 2 x
0 1 2 3 x
0 1 2 3 4 x
0 1 2 3 4 5 x
0 1 2 3 4 5 6 x
0 1 2 3 4 5 6 7 x
0 1 2 3 4 5 6 7 8 x
0 1 2 3 4 5 6 7 8 9
x = 0
while x < 10:
print
x = x + 1
z = 0
while z < x:
print z,
z = z + 1

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

Gerard

Apr 27 '06 #4
try something like this:

x = 0
while x < 10:
z = 0
print '-' + str(x) + '-'
x = x + 1
while z < x:
print '.' + str(z) + '.',
z = z + 1

Apr 27 '06 #5

Dan Sommers wrote:
On 27 Apr 2006 02:48:46 -0700,
mi*******@hotmail.com wrote:
why the output of this code :
x = 0
while x < 10:
z = 0
print x
x = x + 1
while z < x:
print z,
z = z + 1

is

0


Okay, that was x, from the print statement inside the x-loop.
0 1


And that's z, from the print statement inside the z-loop. z is 0, and
then when z is 1, it's not less than x, so we're done printing z's.

And then we get the next x.
0 1 2


And two more z's, 0 and 1, since x is now 2.

And another x.

Since this might be homework, I'll stop at a hint: you need to think
about when you want each printed line to end, and make sure that you
tell python to end it there.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist

thanks..solved...just removed the print x..

Apr 27 '06 #6
On Thu, Apr 27, 2006 at 02:48:46AM -0700, mi*******@hotmail.com wrote:
why the output of this code :
x = 0
while x < 10:
z = 0
print x
x = x + 1
while z < x:
print z,
z = z + 1

is

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 < ---extra
instead of :
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

In your nested loop you are printing an x followed by zero or
more z's, but only after each x, including the first one,
you switch to a new line.
So the last digit on each line is an x, except for the last line,
which are z's printed after the 9 (an x) ending the line above it.

Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
================================================== ======================
Apr 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Crawford | last post: by
1 post views Thread by artifact | last post: by
reply views Thread by Paul Woodsford | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.