can anyone advise me
egbert
egbert.bouwman at hccnet.nl
Thu Apr 27 08:36:07 EDT 2006
On Thu, Apr 27, 2006 at 02:48:46AM -0700, micklee74 at 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
========================================================================
More information about the Python-list
mailing list