Print in tabulated form

345 views Asked by At

How can I print this in tabulated form?

listex = range(nOfYears)
counter = 1
for i in listex:
        if i ==0:

            print counter, '\t',startingBalance,'\t', amountofinterest, '\t', next_balance 
            previous_balance = next_balance
            total_interest +=amountofinterest
        else:
            counter += 1

            amountofinterest = previous_balance*interestRate
            total_balance = previous_balance+amountofinterest
            print counter, '\t', previous_balance,'\t', amountofinterest, '\t', total_balance
            previous_balance = total_balance
            total_interest += amountofinterest 
print '\n', "TOTAL BALANCE IS :", previous_balance
print "TOTAL AMOUNT OF INTEREST IS %.2f:" %(total_interest)
1

There are 1 answers

0
Benoît Latinier On BEST ANSWER

If you want to print this in the shape of a table in console, you should try the tabulate module which is really easy to use. (https://pypi.python.org/pypi/tabulate) Simple example:

from tabulate import tabulate
table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
         ["Moon",1737,73.5],["Mars",3390,641.85]]
print tabulate(table)
#-----  ------  -------------
#Sun    696000     1.9891e+09
#Earth    6371  5973.6
#Moon     1737    73.5
#Mars     3390   641.85
#-----  ------  -------------

If you want to separate columns with \t character, you should check for the csv module and csv writer.