2012-07-24 75 views
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() 

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

回答

0

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

+0

这个工作和我是这么认为的书要我做。上面的代表什么意思? – user1547428 2012-07-24 03:37:48

+0

'str(年)'给你几年的字符串表示。 – BrenBarn 2012-07-24 03:41:16

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 } 
0

输出线应该写成如下:

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

是的,这是我在6分钟前给出的完全相同的答案。 – Aesthete 2012-07-24 03:36:16

2

您可以使用,来分开字符串和值。S(,自动添加一个空格)

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) 
0

我觉得问题你所拥有的就是你在“年”这个词的周围没有逗号。尝试这个。
高清的main(): 打印“这个程序计算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 

希望这个作品:)