这段代码的目的是接收一个字符串并以特定格式打印出来。例如,给定:
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)
谁能看到我的问题是什么?我打印出 external[0] 和 consoleCol 的大小。len(output[0]) 大于 consoleCol。