2012-07-24 45 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 

希望這個作品:)