From what I understand you want to implement a code in Python that is able to assemble two lists - class A and class B - storing the grades of their respective students, in addition to putting together a third list - student AP - storing the grades of the approved students. Then calculate the arithmetic average of grades in class A and class B, calculate the number of grades in each class and display lists and results.
First of all you should realize that the amount of vector values turmaA
is N and the amount of vector values turmaB
is K. This tells us that neither sempres these values will be equal, and there are strong possibilities for them to be different.
One of the right ways to resolve this issue is:
from random import uniform
n = int(input('Número de alunos da turma A: '))
k = int(input('Número de alunos da turma B: '))
turmaA = list()
turmaB = list()
alunosAp = list()
apro_A = 0
for i in range(1, n + 1):
valor1 = round(uniform(0.0, 10.0), 1)
turmaA.append(valor1)
if valor1 >= 5.0:
alunosAp.append(valor1)
apro_A += 1
apro_B = 0
for j in range(1, k + 1):
valor2 = round(uniform(0.0, 10.0), 1)
turmaB.append(valor2)
if valor2 >= 5.0:
alunosAp.append(valor2)
apro_B += 1
media_A = round(sum(turmaA) / len(turmaA), 1)
media_B = round(sum(turmaB) / len(turmaB), 1)
print(f'As notas da turma A são: {turmaA}')
print(f'As notas da turma B são: {turmaB}')
print(f'As notas dos alunos aprovados são: {alunosAp}')
print(f'A média da turma A é "{media_A}" e a quantidade de alunos aprovados é "{apro_A}".')
print(f'A média da turma B é "{media_B}" e a quantidade de alunos aprovados é "{apro_B}".')
Note that the first thing that was done in this code was the library import random
, as well as the method uniform
. The inputs
. The first to capture the number of students in class A and the second to capture the number of students in class B.
Moving on, we’ll have the first block for. This, in turn, will run the range(1, n + 1). And, for each of your interactions, with the method uniform
, a real value shall be generated and rounded to a maximum of 1 decimal place and then added to class. Continuing the code flow, the block if verify whether such value generated in the respective interaction is greater than or equal to 5. If yes, this value will also be added to student Ap and the variable apro_A - variable that counts the number of class approvals - will be incremented in one unit.
Subsequently, the program stream executes the second block for. This, in turn, will perform a procedure equal to the first block for, being the same redirected to class B.
After these operations the code will calculate the arithmetic averages of the two classes and then display the results.
What you’ve done so far?
– Augusto Vasques
I am not able to add the values of the randomized list with the function sum, to realize the average
– Luzia de Jesus
from Random import Choices n = int(input("Enter the value of N: ") list = 1 Quant = n values = range(0, 100) class aA = [Choices(values, k=Quant) for _ in range(list)] print("Class A grades were:",(class))
– Luzia de Jesus
I did it another way too
– Luzia de Jesus
from Random import randint n = int(input("Type the value of N: ") turma = [randint(0, 100) for _ in range(n)] soma = sum(class) print(class) print(f'The sum of the grades in the class is: {sum}')
– Luzia de Jesus