1. 安裝prettytable模塊 pip install prettytable
在Pycharm中的解釋器中調用該模塊的話,需要在該IDE環境下的terminal里安裝,在外部運行py文件的話需要另外再安裝一次,因為兩者的路徑不一致
from prettytable import Prettytable
2. 代碼實例
def FinanceCalculator(): '輸入初始金額和月開銷數,返回剩下的金額、當月的支出數、和最后的支出數' print 'Enter opening balance:', RawBalance = float(raw_input()) Balance = RawBalance print 'Enter monthly payment:', MonthlyPayment = float(raw_input()) print '\t\tAmount\tRemaining' table = PrettyTable(['Pymt#','Paid','Balance']) table.add_row(['---','---','---------']) i = 0 while Balance>0: if i==0: paid = 0.00 elif i>0 and Balance>=MonthlyPayment: paid = MonthlyPayment else: paid = Balance Balance = Balance-paid table.add_row([i,'$%.2f' % paid,'$%.2f' % Balance]) i+=1 table.sort_key(i) table.reversesort=False table.border = 0 print table
3. 輸出效果