Dylan Richards
Dylan Richards

Reputation: 786

How to print several items at once

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."

I'm creating this program to time a speech I have to deliver. The error I'm getting is:

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

SyntaxError: invalid syntax 

Upvotes: 1

Views: 159

Answers (5)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250931

you should use commas to separate the values in print :

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

Upvotes: 2

Raunak Agarwal
Raunak Agarwal

Reputation: 7228

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

Upvotes: 2

Gareth Latty
Gareth Latty

Reputation: 88987

A solution is to concatenate the strings you wish to print:

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

Note the additional call to str() to convert your number into a string, as Python will error if you try to add a number to a string (as it's not clear what you want).

This kind of concatenation is a little ugly though, so you might want to use string formatting instead:

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

Upvotes: 6

inspectorG4dget
inspectorG4dget

Reputation: 113935

You need to somehow concatenate the different pieces of what you want to print. The easiest way to do that in your case is to add a comma:

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

Of course, the more "classical" way to do that is to do string concatentation:

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

Upvotes: 3

mata
mata

Reputation: 69022

you need to conactenate the strings:

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

or:

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

Upvotes: 2

Related Questions