473,395 Members | 1,668 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,395 software developers and data experts.

How to make this Python code print blocks of text?



This code prints output in rows like this:
******
******
******
How to make print in blocks like this?
***** ***** ******
***** ***** ******
***** ***** *****

start= int (raw_input("StartTable?"))
upperlimit= int (raw_input ("FinalTable?"))
cycle= start
while cycle <= upperlimit:
........table= cycle
........counter= 0
........while counter < 10:
...............counter= counter + 1
...............print table, "X", counter, "=", counter * table
........cycle= cycle + 1

The program prints multyply tables like this
4 X1 = 4
4 X2 = 8
5 X 1= 5
5 X 2= 10
6 X1= 6
6 X2= 12
etc.
And needs to print like this:
4 X1 = 4.....5 X 1= 5...... 6 X 1= 1
4 X2 = 8..... 5 x 2=10..... 6 X 2= 12

Thanks in advance for any help
Dec 20 '07 #1
1 3629
On Dec 20, 9:00 pm, chriswilliams <chriswilliam...@yahoo.comwrote:
This code prints output in rows like this:
******
******
******
How to make print in blocks like this?
***** ***** ******
***** ***** ******
***** ***** *****

start= int (raw_input("StartTable?"))
upperlimit= int (raw_input ("FinalTable?"))
cycle= start
while cycle <= upperlimit:
.......table= cycle
.......counter= 0
.......while counter < 10:
..............counter= counter + 1
..............print table, "X", counter, "=", counter * table
.......cycle= cycle + 1

The program prints multyply tables like this
4 X1 = 4
4 X2 = 8
5 X 1= 5
5 X 2= 10
6 X1= 6
6 X2= 12
etc.

And needs to print like this:
4 X1 = 4.....5 X 1= 5...... 6 X 1= 1
4 X2 = 8..... 5 x 2=10..... 6 X 2= 12

Thanks in advance for any help
The reason for it printing on multiple lines is because the print
statement adds a newline character to the end of the existing line.
You can get around this by throwing times table into a list and then
joining them.

for x in xrange(start, (upperlimit+1)):
print '...'.join(['%i X %i = %i'%(x, j, (x*j)) for j in
xrange(1,11)])

Some things to remember, rather use iterators than creating them
yourself with integer variables and incrementing them. Also the
easier syntax for incrementing counters yourself is 'counter += 1'
instead of the uglier 'counter = counter + 1'.
For the string you are printing, look into string formatting rather
than dumping things like 'print table, "X", counter, "=", counter *
table' as "print '%i X %i = %i' % (table, counter, (table*counter))"
is so much neater and easier to comprehend especially if you end up
with a long ass string all muddled together.

HTH,
Chris
Dec 20 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
20
by: Hung Jung Lu | last post by:
Hi, I know people have talked about it before, but I am still really confused from reading the old messages. When I talk about code blocks, I am not talking about Lisp/Ruby/Perl, so I am not...
20
by: Doug Holton | last post by:
Is there any metaclass trick or something similar to allow anonymous code blocks? I'd like to be able to let users do something like this fictitious example: b = Button() b.OnClick =: print...
28
by: The Eternal Squire | last post by:
PEP: 336 Title: Make None Callable Version: $Revision: 1.1 $ Last-Modified: $Date: 2004/11/03 16:58:30 $ Author: Andrew McClelland Status: Draft Type: Standards Track Content-Type: text/plain...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
29
by: dbhbarton | last post by:
Had a thought that's grown on me. No idea if it's original or not- too inexperienced in programming- but I guess there's no harm floating it out there. Python wins big on readability, and...
22
by: Kurien Mathew | last post by:
Hello, Any suggestions on a good python equivalent for the following C code: while (loopCondition) { if (condition1) goto next; if (condition2) goto next;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.