格式化和索引

时间:2015-11-09 02:56:23

标签: python python-2.7 python-3.x

我需要创建一个格式化的表来跟踪每年的投资价值(一列中的年份和另一列中的本金)。我不能使用图形,因为我不知道如何使用。我只需要他们在每个类别下排队。加上我不知道如何循环查找连续的年份和金额。这是我写的:

def main ():
    print ("This program calculates the future value")
    print ("of an investment over a period of time")

    principal = eval(input("Enter the initial principal: "))
    apr = eval(input("Enter the annual interest rate: "))
    years= eval(input("Enter the number of years to maturity: "))

    for i in range(years):
        principal = principal * (1+ apr/100)


    print("Initial principal is", principal)
    print("Initial interest rate is {0:.2f}%".format(apr /100))


    print("Year, " ", Principal")
    print("{0:2.2f}".format(principal))

main ()

1 个答案:

答案 0 :(得分:-2)

不要让人们为你做功课

def main ():
print ("This program calculates the future value")
print ("of an investment over a period of time")

principal = eval(input("Enter the initial principal: "))
apr = eval(input("Enter the annual interest rate: "))
years= eval(input("Enter the number of years to maturity: "))

print("Initial principal is", principal)
print("Initial interest rate is {0:.2f}%".format(apr /100))

print("Year\tPrincipal")
for i in range(years):
    principal = principal * (1+ apr/100)
    print(str(i) + "\t{0:2.2f}".format(principal))

main()