Python如何制作表格

时间:2015-03-09 05:19:12

标签: python loops charts

所以我在下面构建了这个代码,它基本上计算了3个不同主体的复合利息;最小主体,增量主体(用户输入的值+添加到最小主体),最后是最大主体。

principal=int(input("Enter an initial principle value: "))
assert principal >0, "Principal must be a positive integer value"
max_prin=int(input("Enter a maximum principle value: "))
assert max_prin >0 and max_prin>principal, "Principal must be a positive integer value"
increment=int(input("Enter the increment to increase it by: "))
prin_incre=increment+principal
interest=float(input("Enter an interest rate: "))
assert interest >0 and interest <1, "Interest must be greater than 0 but less than 1"
years=int(input("Enter how many years do you want it to go up by: "))
assert years>0, "Years must be a positive integer value"
min_year=int(input("Enter how many years it will take on a minimum basis: "))
assert min_year>0
max_year=int(input("Enter how many years it will take at maximum: "))
assert max_year > min_year and max_year >0

代码基本上获取用户输入和所有断言,以确保输入的值是正确的。

现在底部的代码基本上是在一定年份范围内计算三种不同原则的三种不同复合利息的函数(最小年份到最大年份,用户输入的年份增量)。

def compound(principal, interest, years,min_year,max_year,max_prin,prin_incre,increment):
 print("Compound Interest Table")
    for period in range(min_year,max_year,years):
        total_min = float(principal) * float((1+interest)**float(period+1))
        print('Year:', period+1)
        print('Total: {0:.2f}'.format(total_min))
        total_max=float(max_prin) * float((1+interest)**float(period+1))
        print('Year:', period+1)
        print('Total: {0:.2f}'.format(total_max))
        total_incre=float(prin_incre) * float((1+interest)**float(period+1))
        print('Year:', period+1)
        print('Total: {0:.2f}'.format(total_incre))

现在基本上我遇到的问题是将我的输出放在一个有组织的图表中:

Enter an initial principle value: 1000
Enter a maximum principle value: 2000
Enter the increment to increase it by: 500
Enter an interest rate: 0.1
Enter how many years do you want it to go up by: 1
Enter how many years it will take on a minimum basis: 5
Enter how many years it will take at maximum: 10
Compound Interest Table
Year: 6
Total: 1771.56
Year: 6
Total: 3543.12
Year: 6
Total: 2657.34
Year: 7
Total: 1948.72
Year: 7
Total: 3897.43
Year: 7
Total: 2923.08
Year: 8
Total: 2143.59
Year: 8
Total: 4287.18
Year: 8
Total: 3215.38
Year: 9
Total: 2357.95
Year: 9
Total: 4715.90
Year: 9
Total: 3536.92
Year: 10
Total: 2593.74
Year: 10
Total: 5187.48
Year: 10
Total: 3890.61

正如你所看到的,理想情况下,我试图创造一些类似于例如的东西:

compound interest table
year       original principlal  increment principal  maximum principal
5            454545               8448484              944949
6            555555               etc                  etc
7            994949
8           etc
9           etc
10          etc

因此,根据我上面的代码,如果有人对我如何获取我当前的输出有任何建议或意见,并将其放入我上面直接说明的理想图表输出中,请让我知道它的想法难以置信!谢谢

编辑:只是希望能够将输出模拟为图表而不是可导出的图表。

2 个答案:

答案 0 :(得分:1)

如果列表中包含您的数据列表,则可以使用此简单模块https://code.google.com/p/prettytable/

答案 1 :(得分:0)

我修改了您的代码,以便完成预期的结果。要记住一些变化:

  • 我写了raw_input而不是input,因为如果您使用的是Python 3及以上版本,我会使用Python 2.7恢复输入。
  • 然后,我修改了初始for循环,以将您获得的每个值保存到它自己的列表中。
  • 然后,我在下面的for循环中以表格的形式将列表打印出来。 Edt:修正了间距 - 最后,我运行了你的代码最初没有的功能。

如果您对此代码有任何疑问,请求或疑虑,请发表评论:

principal=int(raw_input("Enter an initial principle value "))
assert principal, "Principal must be a positive integer value"
max_prin=int(raw_input("Enter a maximum principle value "))
assert max_prin, "Principal must be a positive integer value"
increment=int(raw_input("Enter the increment to increase it by" ))
prin_incre=increment+principal
interest=float(raw_input("Enter an interest rate "))
assert interest and interest, "Interest must be greater than 0 but less than 1"
years=int(raw_input("Enter how many years do you want it to go up by "))
assert years, "Years must be a positive integer value"
min_year=int(raw_input("Enter how many years it will take on a minimum basis "))
assert min_year
max_year=int(raw_input("Enter how many years it will take at maximum "))
assert max_year

def compound(principal, interest, years,min_year,max_year,max_prin,prin_incre,increment):
    years_list = []
    total_min_list = []
    total_max_list = []
    total_incre_list = []
    print("Compound Interest Table")
    for period in range(min_year,max_year,years):
        total_min = float(principal) * float((1+interest)**float(period+1))
        total_min_list.append(total_min)
        years_list.append(period+1)
        total_max=float(max_prin) * float((1+interest)**float(period+1))
        total_max_list.append(total_max)
        total_incre=float(prin_incre) * float((1+interest)**float(period+1))
        total_incre_list.append(total_incre)

    print('Year        Total Minimum        Total Maximum        Total Increment')
    for i in range(0,int(len(total_min_list))-1):
        spacing_min = 21 - len(str(total_min_list[i]))
        spacing_max = 21 - len(str(total_max_list[i]))
        spacing_years = 12 - len(str(years_list[i]))
        print(str(years_list[i])+ ' '*spacing_years +str(total_min_list[i])+ ' '*spacing_min +str(total_max_list[i])+ ' '*spacing_max +str(total_incre_list[i]))

 compound(principal,interest,years,min_year,max_year,max_prin,prin_incre,increment)