我正在为我的python课程做一个最终项目,并选择制作一个我可以在玩nd时使用的法术库。(是的,我知道已经有很多了,但我想要我自己喜欢的方式)。
当我的程序打印拼写信息时,现在它将文本包装在单词的中间:
Spell: Acid Splash
Output:
Source: Player's Handbook
Conjuration cantrip
Casting Time: 1 action
Range: 60 feet
Components: V, S
Duration: Instantaneous
You hurl a bubble of acid. Choose one creature you can see within range, or choose two creatures you can see within range that are within 5 feet of e
ach other. A target must succeed on a Dexterity saving throw or take 1d6 acid damage.
At Higher Levels. This spell’s damage increases by 1d6 when you reach 5th level (2d6), 11th level (3d6), and 17th level (4d6).
Spell Lists. Artificer, Sorcerer, Wizard
在这个例子中,它在"each"中间分开。因为这是在我的终端边缘的单词,但其他咒语当然有其他被切断的单词。
我想让它转到换行符前面或后面的单词。
我尝试过textwrapper,但这只会使它不稳定,因为它不考虑现有的换行符。我在本网站上的其他查询中发现的其他解决方案也删除了换行符。
自己滚动这个并不难。以换行符分隔以从文本中获取行。然后把它分成几个单词。跟踪打印单词的长度。如果超过指定的宽度,则打印换行符。然后打印单词后加空格。
在每行的末尾打印一个换行符。
text = """Spell: Acid Splash
Output:
Source: Player's Handbook
Conjuration cantrip
Casting Time: 1 action
Range: 60 feet
Components: V, S
Duration: Instantaneous
You hurl a bubble of acid. Choose one creature you can see within range, or choose two creatures you can see within range that are within 5 feet of each other. A target must succeed on a Dexterity saving throw or take 1d6 acid damage.
At Higher Levels. This spell’s damage increases by 1d6 when you reach 5th level (2d6), 11th level (3d6), and 17th level (4d6).
Spell Lists. Artificer, Sorcerer, Wizard"""
# With a line length of 40 characters
for line in text.split('n'):
words = line.split()
count = 0
for word in words:
count += len(word) + 1
if count > 40:
print()
count = 0
print(f"{word} ", end='')
print()