can anyone advise me

M

micklee74

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
 
D

Dan Sommers

On 27 Apr 2006 02:48:46 -0700,
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

0

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

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.

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
 
P

Peter Otten

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
 
G

Gerard Flanagan

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
 
L

looping

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
 
M

micklee74

Dan said:
On 27 Apr 2006 02:48:46 -0700,


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


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.


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


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

egbert

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
========================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top