Siyah Siyah - 19 days ago 6
Python Question

Making a pyramid shape of a sequence gives error

I can't seem to fix my problem. I have a list of letters I am outputting, which works fine. Now, I want to get it into a pyramid shape...

The error I am getting, is this:

TypeError: can't multiply sequence by non-int of type 'str'


I have noticed that I need to multiply the outcomes with a space, like this:

def pyramid(latest_letter):
for letter in range(102,latest_letter):
print(first_part_of_letters(letter) *' ' + second_part_of_letters(letter-2)) * '* '


I have seen people using this to make a pyramid shape, so I am not getting why this isn't working. Should I convert the sequence to a string first? I don't want to change this function that much. Can someone help me by giving me directions?

Answer

As I believe that it is in continuation to the question you asked How make this for loop work?. I am updating the logic in my original answer that I mentioned there, to generate the pyramid. The idea to get that output to be printed in the form of pyramid is to multiply ' ' based on the position of your alphabet. Hence, it could be written as:

import string
alphs = string.ascii_lowercase   # returns string of lower case characters
last_letter = 'f'
index = alphs.index(last_letter) # get index of the `last_letter`

for i in range(index):
    print ' '*(index-i-1), alphs[:i]+alphs[i::-1]
#          ^ print ` ` blank space based on the value of `index - i-1`

which print the output as:

     a
    aba
   abcba
  abcdcba
 abcdedcba