How to do the inversion of what is received with comma addition?

Asked

Viewed 65 times

0

Program that receives a name via keyboard and must print the last name, first name and other abbreviated names ex.: receives "Luis Costa Santos" and returns "Santos, Luis C."

lista = []
nome = ''
answer = ''
i = 0

while answer != 'n':
    nome = str(input('ponha o seu nome: '))
    lista.append(nome)
    answer = str(input('deseja continuar? s ou n: '))
    if answer == 's':
        i += 1
    else:
        break

print(lista)
  • 1

    Doubt: why are you reading multiple names? The statement asks to read only one, so this loop of repetition made no sense.

  • 1

    Homer, what would the name "So-and-So" look like? I’d stay Silva, Fulano d. or Silva, Fulano D. or Silva, Fulano ??

  • The first option Silva, Fulano D.

2 answers

4


You will need to follow the following steps:

  1. break the full name by spaces (using str.split() for example)
  2. separate the last name (list.pop()) and keep unchanged
  3. separate the first name (list.pop(0)) and keep unchanged
  4. take only the first letter of the remainder of the name
  5. concatenate the answer

Example:

def converte_nome(nome):
    nome = nome.split()

    primeiro_nome = nome.pop(0)
    sobrenome = nome.pop()

    restante = [primeiro_nome] + [n[0] + "." for n in nome]
    restante = " ".join(restante)

    return f"{sobrenome}, {restante}"

print(converte_nome('Luis Carlos Costa'))
# Costa, Luis C.
print(converte_nome('Fernando Sávio Rosback Dominguez'))
# Dominguez, Fernando S. R.
print(converte_nome('João Silva'))
# Silva, João

Code working.

  • is that the "def" function has not been passed in class yet, so I wonder if there would be any way to resolve the issue without it. Thank you very much!

  • names have to be typed, not pre-selected.

  • It’s just an example. You could use the function input instead of using a ready string

  • putting "name = str(input('type the name: '))"before the "def" function, what should I change in the program you made? I’m sorry to ask, but I haven’t worked with "def".

  • I’m on the phone now but you can put your code before the def, delete the line from def, remove the spaces before the body of the function and replace the return by a print

  • VALEU FERNANDO!!!

  • I was able to assemble the improved code

  • list = [] name = ' Answer = ' i = 0 while Answer != 'n': name = str(input('put your name: ')) list.append(name) Answer = str(input('do you want to continue? s or n: ')) if Answer == ’s': i += 1 Else: break print(list) name = name.split() first name_first name = name.pop(0) last name = name.pop() remaining = [first name_name] + [n[0] + "." for n in first name] remainder = "". Join(remainder) print(surname,',', remainder)

  • I removed the repeat loop because when there was more than one name typed it didn’t happen what it should list = [] name = ' Answer = ' i = 0 name = str(input('put your name: ')) list.append(name) print(list) name = name.split() first name_name = first name.pop(0) last name = last name.pop() remainder = [first name_name] + [n[0] + "." for n in first name] remainder = "". Join(remainder) print(surname,',', remainder)

  • I’m glad it worked out Homer. But more importantly, did you manage to understand the code? If you didn’t, I’ll explain it to you better

  • This part of the code got a little confusing for me, because it’s functions I didn’t know: name = first name.split() first name = first name.pop(0) last name = first name.pop() remainder = [first surname] + [n[0] + "." for n in name] remaining = " ". Join(remaining) -

  • The method str.split breaks a string in a list according to the separator passed as parameter. If no parameter is passed, the method uses whitespaces as separator (line break, tab, spaces, etc...). The method list.pop removes an element from the list and returns it for you to use. This method takes as parameter the list index which you want to remove, if no parameter is passed it removes the last element from the list. That’s why my code uses nome.pop(0) to get the first name and nome.pop() to take the last

  • The method str.join() does the reverse of the split. It joins a list using a tab you choose. You can search for comprehensilist on to better understand what I do or ask here on the site. There are a lot of people who understand python here. : D

  • You could use Slices as @nosklo used to pick up parts of the list

  • I tried to use his and I couldn’t

  • thanks!!!

Show 11 more comments

2

The code you passed has almost nothing to do with the question - it just reads the names and adds in a list!

This example takes a name that is in the variable nome and applies what you did. Initially it divides the whole name into separate words, and then treats each word separately:

palavras = nome.split()
novo_nome = ' '.join([  #monta o novo nome, composto de:
    palavras[-1] + ',', # ultimo nome seguido de virgula
    palavras[0],        # primeiro nome
] + [palavra[0] + '.'   # primeira letra seguida de ponto
    for palavra in palavras[1:-1]]) # dos demais nomes do meio

Testing, with nome = "Luis Costa Santos" the result is expected:

Santos, Luis C.

Browser other questions tagged

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