简体   繁体   中英

Why is my print function printing the () and the “” along with the statement?

This is my code for a challenge in Python Programming for the Absolute Beginner

    food = input("What is your favorite entree? ")

    dessert = input("What is your favorite dessert? ")

    print("\nI am going to assume that your favorite meal is" , food +   dessert)

Instead of printing

I am going to assume that your favorite meal is

It is printing

('\nI am going to assume that your favorite meal is', 'steakcookies')

What do I need to change?

You are treating it like Python 3's print() function. When you do this in Python 2, it prints the object you give it, which is a tuple . In Python 3, those parentheses would delineate the function call, with each contained object being an argument passed to the function.

If you want it to behave the way you expect, import the print() function at the top of your code:

from __future__ import print_function

From that point onward in that program, print will refer to the function, not the statement.

You need to take out the parentheses:

print "\nI am going to assume that your favorite meal is" , food +   dessert

In Python2, print is a statement, not a function. When you include the parentheses, print treats the arguments as part of a tuple. You should also be using raw_input() , not input() . Alternatively, put from __future__ import print_function at the beginning of the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM