打印逗号功能不起作用

时间:2012-08-07 06:07:26

标签: python printing

我输入了这段代码:

def game():
down, right = 0, 0
while 1:
    for i in range(down):
        print "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
    for j in range(right):
        print "0",
    print "S",
    for j in range(80 - (right + 1)):
        print "0",
    for i in range(38 - (down + 1)):
        print "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
    direction = raw_input("Direction?")
    if(direction.upper() == "W"):
        down -= 1
    elif(direction.upper() == "S"):
        down += 1
    elif(direction.upper() == "A"):
        right -= 1
    elif(direction.upper() == "D"):
        right += 1
game()

'print“S”,'应该在结尾处有逗号,以便在下次调用打印功能时停止在下一行打印。

实际的事情是这样的:(我切断了一些包含“000000”的行)

S 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000

为什么第一个零之间有空格?我的打印功能没有空格。

2 个答案:

答案 0 :(得分:3)

print "0", 

表示print“0”,print表示空格。

逗号基本上会导致print输出空格而不是换行符。如果您想避开此行为,请改用sys.stdout.write()

答案 1 :(得分:3)

您可以在每False

之后将softspace设置为,
print "S",
sys.stdout.softspace = False

但是你也可以使用sys.stdout.write()