Diagonal name display (Up to Down)

Asked

Viewed 76 times

-3

I have to read a name and display it diagonally, that is, from top to bottom.

But I’m not getting the logic right, I’ve come to this:

nome = input('Digite o seu nome: ')
tamanho = len(nome)

for nome in range(tamanho):
    print(nome)

But it returns only the count of the String size, and I want instead of the count to display the respective letter of the name that the user typed.

  • Can you explain me better please? You want the user to enter a number and the letter that corresponds to this number appears?

1 answer

4

The function range(n) will generate a sequence of integers [0, n[, which is not very useful to you in this case. If I understand correctly, you need to print the name entered on the "diagonal", something like:

>>> Insira seu nome: anderson
a
 n
  d
   e
    r
     s
      o
       n

That is, you increase the number of white spaces before each letter. The first letter, at index 0, has zeros spaces; the second, at index 1, has a space; so on. So, for you to get the letter and its respective index within the name you must use the function enumerate:

nome = input('Insira seu nome:')

for indice, letra in enumerate(nome):
    print(' ' * indice + letra)

And you will have the presentation of the name as exemplified.

  • Good seeing your solution I managed to do in a simpler way, however I did not understand the part of ' print(' ' * Indice + letter) '

  • this was my code name = input('Enter your name: ') for c in name: print(c)

  • @Giovannimanganotti And where the "diagonal" comes in?

  • it will print a letter to each loop, so the name stays diagonal, runs it for you, uses this online compiler ---> https://www.onlinegdb.com/online_python_compiler

Browser other questions tagged

You are not signed in. Login or sign up in order to post.