Ich habe ein Programm (futval.py), das den Wert einer Investition nach 10 Jahren berechnet. Ich möchte das Programm so ändern, dass es nach 10 Jahren nicht den Wert einer einmaligen Investition berechnet, sondern den Wert einer jährlichen Investition nach 10 Jahren. Ich möchte dies tun, ohne eine Akkumulatorvariable zu verwenden. Ist es möglich, dies nur mit den Variablen zu tun, die im ursprünglichen Programm vorhanden waren (Investition, apr, i)?Python - akkumulierter Wert der jährlichen Investition
# 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()
Ich konnte das Programm nicht ändern, ohne die Akkumulatorvariable 'futval' einzuführen.
# 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()
Warum willst du tun, um diesen – Natecat
Es ist ein Problem in einem Python-Lehrbuch. Angeblich ist es möglich, eine Akkumulatorvariable einzuführen, aber ich kann nicht herausfinden, wie. – TexanBruceWayne
Ich bin mir ziemlich sicher, es wäre futval = (Investition * 10) ((1 + apr) ** 10) – Natecat