loop for, and arrays in python

Question:

VALOR_VETOR = 6
nota1 = []
nota2 = []
nota3 = []
mediaAluno = []
soma1 = 0
soma2 = 0
soma3 = 0
somaMedia = 0
mediaTurma = 0
print("Digite as notas dos alunosnn")
for i in range (VALOR_VETOR):
    print(f"Aluno {i}")
    valor = float(input(f"Nota 1: "))
    nota1.append(valor)
    valor = float(input(f"Nota 2: "))
    nota2.append(valor)
    valor = float(input(f"Nota 3: "))
    nota3.append(valor)
    valor = (nota1 + nota2 + nota3)/3
    mediaAluno.append(valor)
    print (f"Nota final = {mediaAluno[i]:.1f}")
for i in range (VALOR_VETOR):
    soma1 = soma1 + nota1
    soma2 = soma2 + nota2
    soma3 = soma3 + nota3
    somaMedia = somaMedia + mediaAluno
mediaProva1 = soma1/VALOR_VETOR
print(f"A media da primeira prova é = {mediaProva1:.1f}")
mediaProva2 = soma2/VALOR_VETOR
print(f"A media da segunda prova é = {mediaProva2:.1f}")
mediaProva3 = soma3/VALOR_VETOR
print(f"A media da primeira prova é = {mediaProva1:.1f}")
mediaTurma = somaMedia/VALOR_VETOR

I am a python learner
try, searched, but could not do.
please help me.

line 23, in
valor = (nota1 + nota2 + nota3)/3
TypeError: unsupported operand type(s) for /: ‘list’ and ‘int’

Asked By: MarceloKade

||

Answers:

  • When you use the ‘+’ operator with lists it will return a list with all the 3
    lists concatenated
    for example:

    l1 = [1,2] 
    l2 = [5,7]
    l  = l1 + l2 #  [1,2,5,4]
    

I think you expect to get l =[6,9]

also the operator ‘/’ is not defined for a list

so l/3 will return an error

if you want to to sum individual elements in a list you can use below code
i used for loop for clarity but you can use list comprehension or lambda or …

l =[]
for i in range(len(l1)):
    l.append(l1[i] + l2[i])
""" to make the division we can use the same for loop or make another 
    loop"""

for x in range(len(l)):
    l[x] = l[x]/3
Answered By: mabdeltawab
 valor = (nota1 + nota2 + nota3)/3  //error

try this line of code as:
valor=(sum(nota1)+sum(nota2)+sum(nota3))/3   //it should work
Answered By: Rahul Mohanty
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.