There are many things that should be considered in your code. In short:
- The indentation is wrong, which would result in syntax error in this case;
- Uses unnecessary repeat loops;
- Makes wrong assignments in
lista3
;
- Returns
lista3.sort()
, and the return of this method is None
at all times;
- Code does not do what is requested;
I will not answer by saying how to solve each point separately, because it is not the objective of the community to give this type of technical support and in the documentation there is enough material to study and understand the differences between the code of the question and the solution presented here.
Consider that we have two lists of people names, sedo that these lists are already in lexicographic (alphabetical) order. We wish to join these two lists in one, maintaining the alphabetical order.
This part of the statement calls for the addition of two lists that are already in order in a third that should also be in alphabetical order. To join two lists just use the operator +
and the method sort
(or sorted
) to classify it alphabetically:
def juntar(lista1, lista2):
lista3 = lista1 + lista2
lista3.sort()
return lista3
Further reading:
In the input data, each line contains the name of a person. On the first line is the size of the first list, followed by the names of the people on this list. Next comes another row with the size of the second list, followed by the names of the people on the second list.
Attention to the excerpt On the first line is the size of the first list, followed by the names of the people on this list, that is, the names will not all be on the same line as you asked in the question, but rather each name will come in a different line depending on the quantity received. So to get the two lists of names you will need to do:
quantidade1 = int(input('Tamanho da primeira lista'))
lista1 = []
for i in range(quantidade1):
lista1.append(input(f'Nome da {i+1}a pessoa'))
quantidade2 = int(input('Tamanho da segunda lista'))
lista2 = []
for i in range(quantidade2):
lista2.append(input(f'Nome da {i+1}a pessoa'))
print(juntar(lista1, lista2))
You can do this in a function to make it more organized and not repeat code:
def juntar_listas(a: list, b: list) -> list:
return sorted(a + b)
def ler_lista_de_nomes() -> list:
quantidade = int(input('Quantidade:'))
lista = []
for i in range(quantidade):
lista.append(input(f'Nome da {i+1}a pessoa'))
return lista
lista1 = ler_lista_de_nomes()
lista2 = ler_lista_de_nomes()
lista3 = juntar_listas(lista1, lista2)
See working on Repl.it