为什么我的打印功能打印()和“"以及声明? [英] Why is my print function printing the () and the "" along with the statement?
问题描述
食物= (你最喜欢的主菜是什么?)
甜点=输入(你最喜欢的甜点是什么?)
print你最喜欢的食物是,食物+甜点)
而不是打印
我会假设你最喜欢的食物是
$ p $这是打印
('\\ \\ n我会假设你最喜欢的食物是','牛排')
什么是否需要改变?
)函数。当你在Python 2中这样做时,它会打印出你给它的对象,它是一个元组
。在Python 3中,这些括号会描述函数调用,每个包含的对象都是一个传递给函数的参数。
如果您希望它的行为符合您的期望,在代码顶部导入
print()
函数:
from __future__ import print_function
从该程序开始,
将引用该函数,而不是语句。
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 atuple
. 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,
这篇关于为什么我的打印功能打印()和“"以及声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!