Python图像创建循环

Python图像创建循环,python,image,loops,Python,Image,Loops,以下代码创建赛道的1D图像: def displayTrack(position): output=''#value given to output track=[' ']*20# track is initially just a bunch of empty spaces track[position]= 'r'#AND track also contains an r icon print(' -'*20)#these are the top and bo

以下代码创建赛道的1D图像:

def displayTrack(position):

    output=''#value given to output
    track=[' ']*20# track is initially just a bunch of empty spaces
    track[position]= 'r'#AND track also contains an r icon
    print(' -'*20)#these are the top and bottom borders
    print('0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J')#these represent each individual cell


    for i in range(len(track)):
        output= output +track[i] +'|'#append a "|" before and after each empty space " "
    print (output)#print the result
    print(' -'*20)
如果运行此代码,您将能够查看图像。如果你看字符“r”,你会发现在字符“r”的右边有“|”字符。我还需要在runner的左侧实现一个“|”。我需要使用与上面类似的方法,因为许多变量和图像的初始状态取决于其他变量,等等

我知道问题存在于输出=''的命运中。如果输出不是空格,或者根本不是字符,那么图像将正确显示,但我不知道如何使其正确显示。谁能帮我一下吗。非常感谢您的帮助

如果有任何不清楚的地方,请告诉我,我会尽快更改

编辑:所以我发现新代码应该是这样的:有3个变化:

1) 输出=“|”而不是“” 2) 在包含连字符和字母数字字符的字符串中,末尾的空格改为移到开头。这就解决了所有问题。

您的评论#在每个空格“”之前和之后添加一个“|”是有误导性的。之前的语句所做的是添加一部分曲目和一个“|”。它不会查看角色是否为空格,也不会在其前面放置任何内容。空格前面有|的唯一原因是它们跟随一个位置,该位置后面有一个


要将某些内容放在其余内容之前,请从output='|'而不是''开始。在这种情况下,您可能需要在其他行之前留出一个额外的空间,以使内容保持对齐。例如:打印(''+'-'*20)

这是您想要的吗?不清楚,因为你原来的布局很奇怪

def displayTrack(position):

    output='|'#value given to output
    track=[' ']*20# track is initially just a bunch of empty spaces
    track[position]= 'r'#AND track also contains an r icon
    print(' -'*20)#these are the top and bottom borders
    print(' 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J')#these represent each individual cell

    for i in range(len(track)):
        output= output +track[i] +'|'#append a "|" before and after each empty space " "
    print (output)#print the result
    print(' -'*20)

是的,我在看这个之前就知道了。新代码很快就会出现在OP中。很抱歉误导了你。