Creation of two dictionaries from a dictionary with one criterion

Asked

Viewed 1,234 times

-3

Read and store in a dictionary the name, age and phone number of your contacts, the key being the name. When typing an empty string for the name, the program stops reading and closes.
Display the read data alphabetically by contact name on the screen. A possible solution to sort alphabetically is to use the Sort method.
Then store the contacts in two other dictionaries, using the age criterion: under 18 years in one and the largest in another dictionary, eliminating the original. Display on screen the two dictionaries resulting from the separation.
Print on the screen a test of your program using as a first registration your name, as telephone your UK, and as age the last two digits of your UK.

If anyone can help me, I’m totally lost in what to do from the first step. I’ve even solved the bold part of the code below. I’m not knowing how to do the bottom, I’ve researched everything that is side, always generates error after error.

lista = []


while True:
    sair = input('Deseja cadastrar um contato ? [S/N]:')
    if sair == ' ':
        break
    nome = input('Qual o nome do contato?:')
    idade = int(input('Qual a idade?:'))
    tel= int(input('Qual o telefone do contato:?'))
    lista.append([nome,idade,tel])

print(lista)

listaordenada = sorted(lista)

for item in listaordenada:
    print('Nome:{}  Idade :{}  Telefone:{}'.format(item[0], item[1], item[2]))
  • 1

    The exercise asks you to store the data in a dictionary in your program is using a list.

1 answer

-3

lista = []
list(lista)
dados = dict()
dadosm18 = dict()
lista18 = []
dados['nome'] = str(input('Nome: ')).title()
while dados['nome'] != '':
    dados['idade'] = int(input('Idade: '))
    dados['telefone'] = int(input('Telefone: '))
    lista.append(dados.copy())
    dados['nome'] = str(input('Nome: ')).title()
print(f'{"NOME":<15}{"IDADE":<10}{"TELEFONE":<10}')
for contatos in lista:
    print(f'{contatos["nome"]:<15}{contatos["idade"]:<10}{contatos["telefone"]:<10}')
if dados['idade'] < 18:
    lista18.append(dados.copy())
    print('-='*60)
    print('LISTA TELEFÔNICA COM DE MAIOR')
    print(f'{"NOME":<15}{"IDADE":<10}{"TELEFONE":<10}')
    for contatos1 in lista18:
        print(f'{contatos1["nome"]:<15}{contatos1["idade"]:<10}{contatos1["telefone"]:<10}')

THE FIRST PART WENT WELL...

Browser other questions tagged

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