1
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 
4

5 回答 5

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))
于 2012-11-03T22:33:23.673 回答
3

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

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

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

print "your introduction is " + str(intro_seconds + (60 * intro_minutes)) + " seconds long."
于 2012-11-03T22:34:40.367 回答
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))
于 2012-11-03T22:34:11.193 回答
2

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

print "your introduction is ", (intro_seconds + (60 * intro_minutes)), " seconds long."
于 2012-11-03T22:34:35.587 回答
2
print "your introduction is %s seconds long" % (intro_seconds + (60 * intro_minutes))
于 2012-11-03T22:35:04.927 回答