Python图像创建循环

2024-09-27 07:27:05 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码创建赛道的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) 在包含连字符和字母数字字符的字符串中,末尾的空格改为移到开头。这解决了所有的问题。你知道吗


Tags: andthe代码图像outputdefpositiontrack
2条回答

您的评论#在每个空格“”前后加一个“|”,会产生误导。前面的语句所做的是添加一部分轨迹和一个“|”。它不看字符是否是空格,也不在它前面放任何东西。在空格前面有|的唯一原因是它们跟在后面有一个的位置后面。你知道吗

若要将某个内容放在其余内容之前,请从output='|'而不是''开始。在这种情况下,您可能需要在其他行之前放置一个额外的空间,以保持事物的排列。例如:print(''+'-'*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)

相关问题 更多 >

    热门问题