-1
Personal I am in doubt on how to write on the screen for the user the values of the dictionary without the bracket and in alphabetical order. I am also doubtful on how to make two new dictionaries to select only contacts aged 18 or older and the other aged 18 or less.
The following is the statement of the question: 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 on the screen the data read in alphabetical order by the name of the contacts. A possible solution to sort alphabetically is to use the Sort method. Then store the contacts in two other dictionaries, using as age criterion: under 18 years in one and the largest in another dictionary, eliminating the original. Present on the screen the two dictionaries resulting from the separation.
My code:
from operator import itemgetter
nomesContatos = {}
for i in range(0, 300):
nome = input('Digite o nome do contato: ')
nome = nome.capitalize()
grade = []
if nome == '':
break
for j in range(0, 1):
while True:
try:
idade = int(input('Digite a idade: '))
grade.append(idade)
break
except ValueError:
print('Idade inválida...')
continue
for k in range(0, 1):
while True:
try:
telefone = int(input('Digite o telefone: '))
grade.append(telefone)
break
except ValueError:
print('Número inválido...')
nomesContatos[nome] = grade
print('\n')
for contatos in sorted(nomesContatos, key=nomesContatos.get):
a = (nomesContatos[contatos])
print(a)