如何在Python中使输入显示在print语句中

如何在Python中使输入显示在print语句中,python,Python,我这里有个错误。如何获取最终打印语句以显示输入值。您在年前后缺少逗号。我假设这是您的问题,但您没有指定您遇到的错误。您在年前后缺少逗号。我假设这是您的问题,但您没有指定您得到的错误 def main(): print "This program calculates the future value of a 10-year investment" principal = input("enter the initial principle: ") years = in

我这里有个错误。如何获取最终打印语句以显示输入值。

您在
年前后缺少逗号。我假设这是您的问题,但您没有指定您遇到的错误。

您在
年前后缺少逗号。我假设这是您的问题,但您没有指定您得到的错误

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()
我认为,应该是:

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

我认为,应该是:

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


输出行应写如下:

print "The amount in %(years) is : %(principal)" % { 'years': years, 'principal': principal }

输出行应写如下:

print "The amount in %(years) is : %(principal)" % { 'years': years, 'principal': principal }

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

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

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


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

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

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


我认为你的问题是“年”这个词周围没有逗号。试试这个。
def main(): 打印“此计划计算10年期投资的未来价值”


希望它能起作用:)

我想你的问题是在“年”这个词周围没有逗号。试试这个。
def main(): 打印“此计划计算10年期投资的未来价值”


希望这能起作用:)

是的,这与我在你之前6分钟给出的答案完全相同。是的,这与我在你之前6分钟给出的答案完全相同。这很有效,也是我认为这本书希望我做的。上面的str代表什么?
str(years)
给出了年份的字符串表示法。这很有效,我想这本书希望我这么做。上面的str代表什么?
str(years)
提供了年的字符串表示形式。
print "The amount in {years} years is: {principal}".format(years=years, principal=principal)
    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