如何一次打印多个项目

时间:2012-11-03 22:30:20

标签: python addition

print "i'm going to calculate minutes for you \n"

print "enter the amount of minutes for your introduction"
intro_minutes = int(raw_input('# '))

print "enter the amount of seconds for your introduction"
intro_seconds = int(raw_input('# ')) 

print "your introduction is "  (intro_seconds + (60 * intro_minutes)) " seconds long."

我正在创建这个程序来定时发表演讲。我得到的错误是:

File "speech.py", line 9
    print "your introduction is " (intro_seconds + (60 * intro_minutes)) " seconds long.)

SyntaxError: invalid syntax 

5 个答案:

答案 0 :(得分:6)

解决方案是连接您想要打印的字符串:

print "your introduction is " + str(intro_seconds + (60 * intro_minutes)) + " seconds long."

请注意额外调用str()将您的数字转换为字符串,因为如果您尝试向字符串添加数字,Python将会出错(因为它不清楚您想要的是什么)。

这种连接虽然有点难看,所以你可能想要使用字符串格式代替:

print "your introduction is {0} seconds long.".format(intro_seconds + (60 * intro_minutes))

答案 1 :(得分:3)

您需要以某种方式连接您想要打印的不同部分。在您的情况下,最简单的方法是添加一个逗号:

print "your introduction is",  (intro_seconds + (60 * intro_minutes)), "seconds long."

当然,更“经典”的方法是做字符串连接:

print "your introduction is " + str(intro_seconds + (60 * intro_minutes)) + " seconds long."

答案 2 :(得分:2)

你需要连接字符串:

print "your introduction is " + str(intro_seconds + (60 * intro_minutes)) + " seconds long."

或:

print "your introduction is %d seconds long." % (intro_seconds + (60 * intro_minutes))

答案 3 :(得分:2)

你应该使用逗号分隔print中的值:

print "your introduction is ", (intro_seconds + (60 * intro_minutes)), " seconds long."

答案 4 :(得分:2)

print "your introduction is %s seconds long" % (intro_seconds + (60 * intro_minutes))