Dicionario Python

Asked

Viewed 1,591 times

-2

Can anyone help me? I’m having a hard time doing this exercise.

Consider the following data set: Name + (N1, N2, N3, N4). Name represents the name of a student and should be used as key. N1, N2, N3, N4 represent the This student’s test notes. Use a dictionary structure with lists to solve this exercise. Write a program that reads the data of N students and present on the screen if gone approved or failed. The criterion guaranteeing approval is that the arithmetic mean of 4 grades greater than or equal to 7.0. The value of N is the number of students, and this value must be read from the keyboard at the beginning of the program. Make a repeat loop for reading these N students. Grades should be displayed at the end of the program with a decimal precision.

This is the code I’m making.

n = int(input('Quantos alunos?  '))
nome = {}
notas = []
i = 0
while i < n:
    nome['Aluno'] = str(input('Nome do aluno: ')).capitalize().strip()
    n1 = float(input('1ª nota:  '))
    notas.append(n1)

    n2 = float(input('2ª nota:  '))
    notas.append(n2)

    n3 = float(input('3ª nota:  '))
    notas.append(n3)

    n4 = float(input('4ª nota:  '))
    notas.append(n4)

    nome['Notas'] = notas

    media = (n1 + n2 + n3 + n4) / 4
    if media >= 7:
        nome['status'] = 'Aprovado'
    else:
        nome['status'] = 'Reprovado'

    i += 1

    continue
for i in nome['Notas']:
    for j, k, l in nome.values():
        print('{} {} {} '.format(nome['Aluno'],nome['Notas'],nome['status'], end=''))

1 answer

2

Well, basically the problem is that you use the same dictionary and the same list to store the information of all students and it gets lost in the middle of your code.

In the statement it is clear that the student’s name should serve as a key to the dictionary: "Name represents a student’s name and should be used as key", but you set two keys, Aluno and Notas.

So, basically it remains to follow what the statement asks:

N = int(input('Quantos alunos? '))

students = {}

for i in range(1, N+1):
  name = input(f'Nome do aluno {i}: ')
  grades = []

  for j in range(1, 5):
    grade = float(input(f'Nota {j} do aluno {i}: '))
    grades.append(grade)

  students[name] = grades

for name, grades in students.items():
  average = sum(grades) / len(grades)
  result = 'aprovado' if average >= 7.0 else 'reprovado'
  print(f'O aluno {name} foi {result} com média {average:.1f}')
  • Thank you so much for your help, I understand where I went wrong. God bless you

Browser other questions tagged

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