小编Jav*_*bos的帖子

Python:避免使用print命令换行

我今天开始编程并且在Python中遇到了这个问题.这是相当愚蠢但我无法弄清楚如何做到这一点.当我使用print命令时,它会打印我想要的任何内容,然后转到另一行.例如:

print "this should be"; print "on the same line"
Run Code Online (Sandbox Code Playgroud)

应该返回:

这应该在同一条线上

而是返回:

这应该
在同一条线上

更确切地说,我试图创建一个程序if,告诉我一个数字是否是2

def test2(x):
    if x == 2:
        print "Yeah bro, that's tottaly a two"
    else:
        print "Nope, that is not a two. That is a (x)"
Run Code Online (Sandbox Code Playgroud)

但是它不会将最后一个识别(x)为输入的值,而是精确打印:"(x)"(带括号的字母).为了使它工作,我必须写:

print "Nope, that is not a two. That is a"; print (x)
Run Code Online (Sandbox Code Playgroud)

如果我输入test2(3)它给出:

不,那不是两个,就是
3

所以要么我需要让Python在打印行中识别我的(x)作为数字; 或打印两个单独的东西,但在同一条线上.在此先感谢并抱歉这样一个愚蠢的问题.

重要说明:我使用的是2.5.4版

另一个注意事项:如果我print "Thing" , print "Thing2"在第二次打印时说"语法错误".

python printing newline python-2.5

132
推荐指数
5
解决办法
40万
查看次数

标签 统计

newline ×1

printing ×1

python ×1

python-2.5 ×1