can anyone advise me

Gerard Flanagan grflanagan at yahoo.co.uk
Thu Apr 27 06:27:10 EDT 2006


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
>

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




More information about the Python-list mailing list