格式化打印:布尔问题



这段代码的目的是接收字符串并以特定格式打印出来。例如,给定:

s = "Hello"

程序应打印出来:

+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+

如果字符串的大小大于控制台的列大小,则应该以以下格式打印字符串:

+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+

不幸的是,第二种情况不起作用,我似乎不明白为什么。

这是我的代码:

import os
s = "Hello"*20
(consoleRows,consoleCol)=os.popen('stty size','r').read().split()
top = outer = "+---"*len(s)+'+'+'n'
for i in range(len(s)):
    outer += "| "+s[i]+" "
outer += '|n'
outer += top[:len(top)-1]
split = outer.split('n')
if(len(split[0]) > consoleCol): #problem lies on this line. Even though the size of  
    outer = outer.split('n') #split[0] is greater than consoleCol the if statement  
    beg = 0   #isn't entered.                                                        
    size = consoleCol
    print(outer[0][beg:size])
    while(size < len(outer[0])):
        print(outer[1][beg:size]);
        print(outer[2][beg:size]);
        beg = size
        size += size
else:
    print(outer)

有人能看到我的问题是什么吗?我打印出了外层[0]和consoleCol的尺寸。len(output[0])大于consoleCol。

代码中的错误:consoleCl是一个字符串,而不是整数。

(consoleRows,consoleCol)=os.popen('stty size','r').read().split()替换为(consoleRows,consoleCol)=map(int, os.popen('stty size','r').read().split())

这仍然没有产生所需的结果——你必须自己解决剩下的问题,也许还要在这个过程中清理代码。

首先,我不是蟒蛇。也就是说,我建议将逻辑与布局分开。创建方法drawHorizontalLinedrawContentLine(Char[5])Char[5][] splitContent(String)。之后的调试应该很容易。

最新更新