Python中的右三角形词和数字3

时间:2017-03-05 08:15:48

标签: python

我正在尝试制作一个像这样的直角三角形词:

通过输入:"计算机学院"

我想绘制这个形状:

F
a c     
u l t 
y _ o f
c o m p u 
t e r _ _ _

我的代码是:

word = input("What's your words?")
counter = 0
maxLINE = 0
maxCHAR = 0

while counter < len(word):
    for line in range(len(word)):
        for idx in range(line):
            maxLINE = maxLINE+1

        for column in range(line):
            print(word[counter], end=" ")
            counter = counter+1

        maxCHAR = maxCHAR + maxLINE
        maxLINE = 0
        print()

        if (line >=7):
            break

我收到错误消息:

  

追踪(最近一次呼叫最后一次):

     

文件&#34; C:/Users/Lenovo/PycharmProjects/Lab03/int.py",第10行,中
  print(word [counter],end =&#34;&#34;)

     

IndexError:字符串索引超出范围

我希望通过任何字符串输入形成完美的直角三角形,任何人都可以给出一些建议吗?

由于

1 个答案:

答案 0 :(得分:0)

试试这个(代码的变体):

word = 'Faculty of computer'
line = 0
start = 0
end = 0
suffix =  ''
done = False
while not done:
    start = end
    while word[start] == ' ':
        start  += 1
    end = start + line + 1
    if end > len(word):
        suffix = '_' * (end - len(word))
        end = len(word)
        done = True
    print(word[start:end].replace(' ', '_') + suffix)
    line += 1

<强>输出:

F
ac
ult
y_of
compu
ter___