当我注释掉for循环时,一切正常。但是,IDLE一旦再次激活,就会发出语法错误。我已经尝试过适当地将其间隔开,因为这是其他问题的建议之一,但是不管我按回车的次数多少,这都行不通。
# futval.py
# A program to compute the value of an investment
# carried 10 years into the future
#begins main function of program
def main():
#description of program
print("This program calculates the future value")
print("of a 10-year investment.")
#creates and assign three variables to user input
principal = eval(input("Enter the initial principal: "))
apr = eval(input("Enter the annual interest rate: "))
compound = eval(input("How often is the interest compounded: "))
#begins a 'for' loop that iterates 10 times indicative of 10 years
for i in range(10):
print (principal)
principal = principal * (1 + (apr/compound)
#prints out the final result of the above equation.
print("The value in 10 years is:",principal)
#original program exited too fast and didn't allow user to see output
#I added the following line so the user could see.
print(input("Press enter to exit."))
main()
答案 0 :(得分:1)
您在)
之后省略了(apr/compound)
for i in range(10):
print (principal)
principal = principal * (1 + (apr/compound))
答案 1 :(得分:0)
语法错误是您缺少一个右括号:
更改principal = principal * (1 + (apr/compound)
到principal = principal * (1 + (apr/compound))
此外,您应该摆脱使用eval
的习惯,因为它通常被认为是危险的,在这里绝对不需要。只需将类型转换为类型并捕获异常即可。
答案 2 :(得分:0)
您的代码缺少括号。否则,它将正常工作。
您的代码:
principal = principal * (1 + (apr/compound)
它应该是:`
principal = principal * (1 + (apr/compound))`