为什么我的打印功能打印()和“"以及声明? [英] Why is my print function printing the () and the "" along with the statement?

查看:99
本文介绍了为什么我的打印功能打印()和“"以及声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 食物= (你最喜欢的主菜是什么?)

甜点=输入(你最喜欢的甜点是什么?)

print你最喜欢的食物是,食物+甜点)

而不是打印

 我会假设你最喜欢的食物是

 ('\\ \\ n我会假设你最喜欢的食物是','牛排')

什么是否需要改变?

)函数。当你在Python 2中这样做时,它会打印出你给它的对象,它是一个元组。在Python 3中,这些括号会描述函数调用,每个包含的对象都是一个传递给函数的参数。



如果您希望它的行为符合您的期望,在代码顶部导入 print()函数:

  from __future__ import print_function 

从该程序开始, print 将引用该函数,而不是语句。


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.

这篇关于为什么我的打印功能打印()和“"以及声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆