仅在确定循环中打印最后一个值

0 python loops

如何在一定范围内打印最后一个值?

def main():
 print "This program calculates the future value of a 10-year investment."

 principal = input("Enter the initial principle: ")
 apr = input("Enter the annual interest rate: ")

 for i in range(10):
  principal = principal * (1 + apr)
  print "The value in 10 years is:", principal 
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
The value in 10 years is XXXXXXX
Run Code Online (Sandbox Code Playgroud)

如何打印循环的最后一次迭代?

jam*_*lak 6

取消最后一行(请不要再使用1个空格缩进,PEP-8建议4个空格)

def main():
    print "This program calculates the future value of a 10-year investment."

    principal = input("Enter the initial principle: ")
    apr = input("Enter the annual interest rate: ")

    for i in range(10):
        principal = principal * (1 + apr)
    print "The value in 10 years is:", principal 
Run Code Online (Sandbox Code Playgroud)