我需要輸入一個句子並在該句子周圍製作動態邊框。邊框需要有一個輸入的寬度。當句子的長度高於給定寬度時,必須打印新行並且邊界必須在高度處更改。這句話也有在動態邊界圍繞Python中文本的動態邊框
我已經嘗試過這個爲中心:
sentence = input()
width = int(input())
length_of_sentence = len(sentence)
print('+-' + '-'*(width) + '-+')
for letter in sentence:
print('| {0:^{1}} |'.format(letter, width - 4))
print('+-' + '-'*(width) + '-+')
但隨後印有一個新行每個字母這不是我所需要的。
一個很好的例子如下:
輸入
sentence = "You are only young once, but you can stay immature indefinitely."
width = 26
輸出
+----------------------------+
| You are only young once, b |
| ut you can stay immature i |
| ndefinitely. |
+----------------------------+
我知道有已經發布解決此主題的問題,但他們沒有覆蓋動態輸入。 –
看看[每隔第n個字符分割python字符串?](http://stackoverflow.com/questions/9475241/split-python-string-every-nth-character)。這應該讓你開始。 –
@DriesCoppens:那又怎麼樣?它不應該有所作爲。 –