473,396 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 913
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: vic | last post by:
My manager wants me to develop a search program, that would work like they have it at edorado.com. She made up her requirements after having compared how search works at different websites, like...
3
by: Michael Crawford | last post by:
Hi, Where would one start for this type of application: I want to create an vb.net container application that has the gives the end user the ability to install and uninstall plugins or add-in...
1
by: artifact | last post by:
Hello Can anyone tell me what is the best way to persist a complex object across postbacks in ASP.NET eg * I have a Person class with methods to edit the person's info * I navigate to my...
7
by: SQLDBA | last post by:
I am in the process of evaluating some SQL Performance Monitoring /DBA tool to purchase (For SQL Server 2000). I have the following list of software that I came across and have to finalize which...
1
by: Abhishekyerra | last post by:
Hi, Can anyone helpone in running db2 advise on the queries and finding out the optimising techinques
0
by: Paul Woodsford | last post by:
Win XP sp2 fully updated with net3 installed. When I click on the Control Panel icon for Windows Cardspace I get an error: " Class not Registered" Can anyone advise. Have reinstalled net3 to no...
0
by: User | last post by:
Hi everyone, Right now, one of our client have an in-house system which stores customer profiles / data. We are building an external web based system for them which also houses the same...
0
by: Eric | last post by:
I have noticed an odd problem and I'm wondering if anyone can shed light on it. Perhaps it is a bug, or maybe it is a characteristic of the QuickSort feature that I wasn't aware of. I have a...
0
by: rsyCoder | last post by:
I am faced with a decision about a design I would like to get right the first time. So a straight up answer or any helpful advice will be well appreciated. We are going to do an inventory. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.