要实现的效果如下图所示:
我用的Python3.5版本,程序代码如下:
#!/usr/bin/env python3.5
sentence = input("Sentence: ")
text_width = len(sentence)
box_width = text_width + 8
screen_width = 8
whole_width = 2 * text_width
left_width = (whole_width - box_width) // 2
print(' ' * screen_width + '+' + '-' * whole_width + '+')
print(' ' * (screen_width + left_width) + '|' + ' ' * box_width + '|' + ' ' * left_width)
print(' ' * (screen_width + left_width) + '|' + ' ' * 4 + sentence + ' ' * 4 + '|' + ' ' * left_width)
print(' ' * (screen_width + left_width) + '|' + ' ' * box_width + '|' + ' ' * left_width)
print(' ' * screen_width + '+' + '-' * whole_width + '+')