我有一个程序 (futval.py) 可以计算 10 年后的投资价值。我想修改程序,以便在 10 年后计算一次投资的价值,而不是计算一次投资的价值,而是在 10 年后计算每年投资的价值。我想在不使用累加器变量的情况下做到这一点。是否可以仅使用原始程序中存在的变量(投资,apr,i)来执行此操作?
# futval.py
# A program to compute the value of an investment
# carried 10 years into the future
def main():
print "This program calculates the future value",
print "of a 10-year investment."
investment = input("Enter the initial investment: ")
apr = input("Enter the annual interest rate: ")
for i in range(10):
investment = investment * (1 + apr)
print "The value in 10 years is:", investment
main()
如果不引入“futval”累加器变量,我无法完成对程序的修改。
# futval10.py
# A program to compute the value of an annual investment
# carried 10 years into the future
def main():
print "This program calculates the future value",
print "of a 10-year annual investment."
investment = input("Enter the annual investment: ")
apr = input("Enter the annual interest rate: ")
futval = 0
for i in range(10):
futval = (futval + investment) * (1+apr)
print "The value in 10 years is:", futval
main()