擅长:python、mysql、java
<p>尽管这是一个直接的答案,但我对Prune的答案进行了扩展,并对您的代码进行了一些更正:</p>
<p>这样做的目的是去除不必要的打印内容,使其更加清晰。你知道吗</p>
<pre><code>lorumIpsum = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source."
def OutputStringToBook(outStr):
# Take each character
for i, item in enumerate(outStr):
# If the character is at the designated line end
if (i % 30 == 0):
# If the current or next character is not a space
if(item == " " or outStr[i+1] == " "):
print("-", end='')
print()
print(item, end='')
OutputStringToBook(lorumIpsum)
</code></pre>