python字符串垂直输入_垂直打印字符串-Python3.2

我正在编写一个脚本,它将作为用户输入的字符串,并将其垂直打印,如下所示:input = "John walked to the store"

output = J w t t s

o a o h t

h l e o

n k r

e e

d

我写了大部分代码,如下所示:import sys

def verticalPrint(astring):

wordList = astring.split(" ")

wordAmount = len(wordList)

maxLen = 0

for i in range (wordAmount):

length = len(wordList[i])

if length >= maxLen:

maxLen = length

### makes all words the same length to avoid range errors ###

for i in range (wordAmount):

if len(wordList[i]) < maxLen:

wordList[i] = wordList[i] + (" ")*(maxLen-len(wordList[i]))

for i in range (wordAmount):

for j in range (maxLen):

print(wordList[i][j])

def main():

astring = input("Enter a string:" + '\n')

verticalPrint(astring)

main()

我很难弄清楚如何使输出正确。我知道for循环有问题。它的输出是:input = "John walked"

output = J

o

h

n

w

a

l

k

e

d

有什么建议吗?(另外,我希望print命令只使用一次。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值