473,586 Members | 2,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Sta rtTable?"))
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 3639
On Dec 20, 9:00 pm, chriswilliams <chriswilliam.. .@yahoo.comwrot e:
This code prints output in rows like this:
******
******
******
How to make print in blocks like this?
***** ***** ******
***** ***** ******
***** ***** *****

start= int (raw_input("Sta rtTable?"))
upperlimit= int (raw_input ("FinalTable?") )
cycle= start
while cycle <= upperlimit:
.......table= cycle
.......counter= 0
.......while counter < 10:
..............c ounter= counter + 1
..............p rint 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
33661
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
20
5920
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 making comparisons. If you have a file, in Python you could do:
20
1994
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 "you clicked me" But that would require adding a special "=:" operator to bind a code block to a function.
28
2337
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 Created: 28-Oct-2004 Post-History:
28
2927
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! PEP: 359 Title: The "make" Statement Version: $Revision: 45366 $ Last-Modified: $Date: 2006-04-13 07:36:24 -0600 (Thu, 13 Apr 2006) $
7
2684
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 the goals -- the make statement is mostly syntactic sugar for:: class <name> <tuple>: __metaclass__ = <callable>
26
2697
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 locally-scoped-together code, in Ruby a "block" defines a closure (anonymous function). To avoid confusion let's call them Ruby block-closures. I see...
29
2335
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 there's no doubt that context- dependent text formatting in IDEs (keywords, strings, comments etc) is a massive help too, therefore benefitting...
22
7899
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
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.