【发布时间】:2016-03-31 23:56:03
【问题描述】:
这是我为绝对初学者准备的 Python 编程挑战的代码
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)
代替打印
I am going to assume that your favorite meal is
正在打印
('\nI am going to assume that your favorite meal is', 'steakcookies')
我需要改变什么?
【问题讨论】:
-
print不是函数,您不要在参数周围加上括号。所以括号正在创建一个列表,它正在打印列表。 -
@Barmar:一个元组,而不是一个列表。
-
通常我们会看到相反的问题——人们试图在 Python 3 上使用 Python 2 语法,却被 SyntaxError 弄糊涂了。
-
在 python 2.7 中,当你使用 print(some, text, here) 时,它会打印一个以 some, text 和 here 作为元组成员的元组。您可以通过不使用括号来解决此问题,因此:
标签: python python-2.7 function printing