[Tutor] Help - International School in Spain

Peter Otten __peter__ at web.de
Tue Nov 17 11:07:49 EST 2015


caroline metcalf wrote:

> Hi there
> 
> I am having an issue.  This code normally works fine
> 
> Code:
> veg="carrots"
> print("Mashed ",veg," on the ceiling.")
> print("Green beans on the floor.")
> print("Stewed tomatoes in the corner.")
> print("Squash upon the door.")
> 
> But, I am getting this.  Result:
> ('Mashed ', 'carrots', ' on the ceiling.')
> Green beans on the floor.
> Stewed tomatoes in the corner.
> Squash upon the door.
>>>>
> 
> I have tried semi-colons instead of commas as Spain uses these often
> but...
> I don't know what else to try.  Any ideas please?

The code you show is written for Python 3 where print() is a function, but 
you run it under Python 2 where print was a statement and the line

> print("Mashed ",veg," on the ceiling.")

tells Python to print the tuple

("Mashed ",veg," on the ceiling.")

instead of invoking the print() function. As a fix I recommend that you 
ensure that you use a Python 3 interpreter. Your other options are:

1. Remove the parens:

print "Mashed ",veg," on the ceiling." # python 2 only

2. Add

from __future__ import print_function # make print() available in py2

3 Use string formatting:

print("Mashed {} on the ceiling.".format(veg))

Parens around a single value are ignored, so here print works both as a 
statement and as a function.




More information about the Tutor mailing list