3

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)
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
abdullah
  • 145
  • 1
  • 1
  • 8
  • 2
    possible duplicate of [Python: Printing Lists as Tabular Data](http://stackoverflow.com/questions/9535954/python-printing-lists-as-tabular-data) – Bhargav Rao Dec 17 '14 at 11:57
  • Instead of dumping a lot of code, be more precise about your actual goal. What do you mean by `this` and what by `tabulated form`? – Wolf Dec 17 '14 at 12:05

1 Answers1

3

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.

Benoît Latinier
  • 2,062
  • 2
  • 24
  • 36