Giovanni
Giovanni

Reputation: 99

Python how to make a table

So I`ve build this code below which basically calculates the compounded interest for 3 different principal; minimal principal, incremental principal(which is a value user inputs +add to minimal principal) and lastly maximum principal.

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

The code basically gets the user inputs and all the assertions to make sure the values inputted are correct.

The code now at the bottom is basically the function which calculates the three different compounded interest on the three different principles over a range of years(minimum year till maximum year with the increment of year inputted by user).

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))

Now essentially the issue I am having is putting my output below into an organized chart:

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

As you can see Ideally I`m trying to create something which will be like for example:

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

Thus given the code I have above, if anyone has any suggestions or comments on how I can take my current output that I have and put it into the ideal chart form output that I stated directly above please let me know its been mind boggling! Thank you

Edit: Just want to be able to mold output to look like a chart and not be a exportable chart.

Upvotes: 1

Views: 543

Answers (2)

Orren Ravid
Orren Ravid

Reputation: 580

I have revised your code so that it accomplishes the intended result. A few changes to keep in mind:

  • I wrote raw_input instead of input because I am using Python 2.7 if you are using Python 3 and above revert back to input.
  • I then modified the initial for loop to save every value you get into a list of it's own.
  • I then printed the list back out in the form of a table in the for loop below it. Edt: Fixed the spacing -Finally, I ran the function which is something your code initially did not have.

If you have any questions, requests, or concerns about this code please leave a comment:

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)

Upvotes: 0

reticentroot
reticentroot

Reputation: 3682

If you have the list your data in list you can use this simple module https://code.google.com/p/prettytable/

Upvotes: 1

Related Questions