Does anyone know how to create this program?

Asked

Viewed 114 times

-6

Write a Python script to create an array turmaA with N real values and a vector turmaB with K real values, containing the grades of the students of the two classes, whose grades should be generated randomly with values in the range of 0.0 a 10.0 (Ex.: 2.4, 8.7, 5.5, etc).

It is requested:

  • a) Calculate the arithmetic mean of the turmaA and the arithmetic average of the turmaB;
  • b) Calculate the number of passed in each class (grade of 5 or more);
  • c) Create a student vectorAp with the student grades passed in both classes. Print the vectors turmaA, turmaB and student Ap, as well as the average of each class and the number of approved students.
    from random import choices 
    n = int(input("Digite o valor de N: ")) 
    lista = 1 
    quant = n 
    valores = range(0, 100) 
    turmaA = [choices(valores, k=quant) for _ in range(lista)] 
    print("As notas da turma A foram:",(turmaA)) 

Tip: Generate integer values from 0 to 100 and divide by 10

  • What you’ve done so far?

  • I am not able to add the values of the randomized list with the function sum, to realize the average

  • 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))

  • I did it another way too

  • 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}')

2 answers

0

See if that helps you:

import random

quantidade = int(input("Digite a quantidade de elementos: "))

turmaA = []
turmaB = []
aprovados = []

mediaTurmaA = 0.0
mediaTurmaB = 0.0
notaCorte = 5.0

quantidadeAprovadosA = 0
quantidadeAprovadosB = 0

# Preencher os vetores com as notas
for i in range(quantidade):
    turmaA.append(random.uniform(0.0, 10.0))
    turmaB.append(random.uniform(0.0, 10.0))
    
# Calcular média da turma A
for i in turmaA:
    mediaTurmaA += i

mediaTurmaA /= quantidade

# Calcular média da turma B    
for i in turmaB:
    mediaTurmaB += i
    
mediaTurmaB /= quantidade

for i in range(quantidade):
    if (turmaA[i] >= notaCorte):
        aprovados.append(turmaA[i])
        quantidadeAprovadosA += 1
        
    if (turmaB[i] >= notaCorte):
        aprovados.append(turmaB[i])
        quantidadeAprovadosB += 1

# Imprimir tudo
print("   TurmaA: ", turmaA)
print("    Média: ", mediaTurmaA)
print("Aprovados: ", quantidadeAprovadosA)
print("\n")
print("   TurmaB: ", turmaB)
print("    Média: ", mediaTurmaB)
print("Aprovados: ", quantidadeAprovadosB)
print("\n")
print("Turma Aprovada: ", aprovados)
  • Take it easy on the criticism, I don’t program in Python. Making this code was quite a challenge :D .

0

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.

Browser other questions tagged

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