473,372 Members | 859 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,372 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 3625
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;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.