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

    principal = input("enter the initial principle: ")
    years = input("enter number of years you want to invest: ")
    apr = input("Enter the APR: ")

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

        print "The amount in" years "years is: " , years, principal

main()

我在这里遇到错误。我如何获得最终的打印语句以显示输入值。

4

5 回答 5

2

您可以使用,分隔字符串和值。(,自动添加空格)

print "The amount in", years, "years is:", principal

但更好的方法是使用字符串格式:

print "The amount in {0} years is: {1}".format(years, principal)

或者:

print "The amount in {years} years is: {principal}".format(years=years, principal=principal)
于 2012-07-24T03:35:25.150 回答
1
print "The amount in" years "years is: " , years, principal

我认为,应该是:

print "The amount in" + str(years) + "years is: " + str(principal)

或者:

print "The amount in %(years) is : %(principal)" % { 'years': years, 'principal': principal }
于 2012-07-24T03:25:38.753 回答
0

我认为您遇到的问题是“年”一词周围没有逗号。尝试这个。
def main(): print "这个程序计算 10 年投资的未来价值"

    principal = input("enter the initial principle: ")
    years = input("enter number of years you want to invest: ")
    apr = input("Enter the APR: ")

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

        print "The amount in",years,"years is:", principal

希望这有效:)

于 2013-01-01T14:48:57.223 回答
0

您缺少逗号years。我认为这是您的问题,但您没有指定遇到的错误。

于 2012-07-24T03:23:35.833 回答
0

输出行应该写成如下:

print "The amount in " + str(years) + " years is: " + str(principal)
于 2012-07-24T03:31:45.200 回答