我正在为 CS 类编写程序,需要一些 python 3 编码方面的帮助。这是我目前编写的代码:
def main():
print() # blank line
phrase = input("Please enter a phrase: ")
wordlist = phrase.split()
print("Original text:",phrase)
for msg in wordlist:
print(msg)
输出:
Phil
likes
to
code
我不能使用任何导入或类似的东西。我只能使用循环、切片或拆分等小东西。任何帮助,将不胜感激。我需要输出如下所示:
P l t c
h i o o
i k d
l e e
s
请您参考如下方法:
您可以使用 itertools.zip_longest() 并将填充值作为空格。示例 -
>>> s = "Four score and seven years ago"
>>> ls = s.split()
>>> import itertools
>>> for i in itertools.zip_longest(*ls,fillvalue=' '):
... print(*i)
...
F s a s y a
o c n e e g
u o d v a o
r r e r
e n s
itertools.izip_longest 适用于 Python 2。
