can anyone advise me

2006-04-27 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can anyone advise me

2006-04-27 Thread Dan Sommers
On 27 Apr 2006 02:48:46 -0700,
[EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can anyone advise me

2006-04-27 Thread Peter Otten
[EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can anyone advise me

2006-04-27 Thread Gerard Flanagan
[EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can anyone advise me

2006-04-27 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can anyone advise me

2006-04-27 Thread micklee74

Dan Sommers wrote:
 On 27 Apr 2006 02:48:46 -0700,
 [EMAIL PROTECTED] 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..

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can anyone advise me

2006-04-27 Thread egbert
On Thu, Apr 27, 2006 at 02:48:46AM -0700, [EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list