0

我输入了这段代码:

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",' 应该在末尾有一个逗号,以便在我下次调用 print 函数时停止在下一行打印。

实际的事情是这样的:(我已经砍掉了一些包含“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 0 0 000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000

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

4

2 回答 2

3
print "0", 

表示print“0”和print一个空格。

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

于 2012-08-07T06:09:21.817 回答
3

您可以将 softspace 设置False,

print "S",
sys.stdout.softspace = False

但是你不妨使用sys.stdout.write()

于 2012-08-07T06:29:17.253 回答