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