Print data from a list that is inside a dictionary - Python

Asked

Viewed 456 times

0

Consider the following data set: Name + (N1, N2, N3, N4). Name represents a student’s name and should be used as a key. N1, N2, N3, N4 represent that student’s test scores. Use a dictionary structure with lists to solve this exercise. notes should be displayed at the end of the program with a decimal precision.

I need to print out the name of what is received via input, and your notes which are also, but I have no idea how I do that. I wanted you to return something like:

Jay 1.0 3.0 4.0 5.0

The code is like this:


for i in range(1):
    nome = input('Digite o nome do aluno: ')
    n1 = float(input('Digite a primeira nota: '))
    n2 = float(input('Digite a segunda nota: '))
    n3 = float(input('Digite a terceira nota: '))
    n4 = float(input('Digite a quarta nota: '))
    alunos['Nome'].append(nome)
    alunos['N1'].append(n1)
    alunos['N2'].append(n2)
    alunos['N3'].append(n3)
    alunos['N4'].append(n4)

for nome, nota in alunos.items():
    print(nome, nota)```

2 answers

1

Analyzing the code, I reached another way, but I still can’t print on the screen the way you ask in the statement:

boletim = {'nome':[], 'n1':[], 'n2':[], 'n3':[], 'n4':[]}

num = int(input('Qual o número de alunos? '))
for i in range(num):
    nome = input('Digite o nome do aluno: ')
    n1 = float(input('Digite a primeira nota: '))
    n2 = float(input('Digite a segunda nota: '))
    n3 = float(input('Digite a terceira nota: '))
    n4 = float(input('Digite a quarta nota: '))
    boletim['nome'].append(nome)
    boletim['n1'].append(n1)
    boletim['n2'].append(n2)
    boletim['n3'].append(n3)
    boletim['n4'].append(n4)
print('\nNota dos alunos')
print('-' * 90)
  • I’m moving that statement to the question.

1

To resolve this issue you should pay attention to the following logic:

  1. Capture the person’s name;
  2. Make a list of the person’s notes;
  3. Feed dictionary with name and notes;
  4. View key, dictionary value.

One of the ways to resolve this issue is:

nome = input('Digite o nome: ')

nome_nota = dict()
notas = list()
for i in range(1, 5):
    notas.append(float(input(f'Digite a {i}º nota: ')))

nome_nota.update({nome: notas})

for j, k in nome_nota.items():
    print(j, *k)

Note that when executing the code we must enter the name of the person. Then we must enter each of the four notes to be inserted in the list notes.

Observing: To add values in a list we use the method append() and to add key/value in dictionary we use the method update().

After the block for insert all values in the list notes will be added name and notes to the dictionary.

Later the second block for will go through key/value of the dictionary displaying respectively the key and the value. As the value is formed by elements of a list we must unpack them, what we get by inserting the sign of (*) - asterisk - immediately before the list notes.


There is another more concise way to resolve this issue using the concepts of Dictionary comprehensions. This way we can implement the following code:

nome = input('Digite o nome: ')
nome_nota = {nome: [float(input(f'Digite a {i}º nota: ')) for i in range(1, 5)]}

for j, k in nome_nota.items():
    print(j, *k)

Note that in this code a Dictionary Comprehension. Where the key corresponds to name and the value corresponds to the list assembled with the aid of List Comprehensions in which each element is the value of each of the student’s 4 grades.

Browser other questions tagged

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