Function to calculate the average of two notes in python

Asked

Viewed 612 times

-1

I have difficulty creating two functions: one that calculates the average of 2 grades and another that tells in which situation the student is "approved", "failed", and "recovery".

My code is giving the following error:

Traceback (Most recent call last): File "main.py", line 18, in media = mediax(i,j) Nameerror: name 'i' is not defined

Code:

def mediax(x,y):
medias = (x + y)/2
return medias

a = 0
while(a != 'fim'):
 nome = input("Nome do aluno")
 mat =  int(input("informe sua matricula"))
 ano = int(input("informe o ano"))
 turma = str(input("informe a turma"))
 nota1 = float(input('entre com a primeira nota'))
 nota2 = float(input('entre com a segunda nota'))
 media = mediax(i,j)
 print(mediax(i,j))
 if media >60:
   print("aluno aprovado")

 if (media>=30) and (media<=60):
   print ('aluno em recuperação')

 if (media<30):
   print('aluno reprovado')
 a = str (input('para sair digite (fim) para continuar (enter)'))
  • is simple, as says error "i" and "j" are not defined, should be media = mediax(nota1,nota2), which are the variables that are defined

1 answer

0

You’re calling mediax(i, j), but where are these variables i and j? Nowhere. You are trying to use variables that do not yet exist, hence the error.

You have to pass the notes, ie, mediax(nota1, nota2). Also, if you saved the value returned by the function in a variable, why call it again in the print?

media = mediax(i,j)
print(mediax(i,j)) # para que chamar a função de novo?

Instead, print the variable that has already been created. There is no reason to call the function 2 times to recalculate something that has already been calculated:

media = mediax(nota1, nota2)
print(media)

Another detail is that input already returns a string, so do str(input(...)) is redundant and unnecessary. If you want a string, just do input(...) suffice.

And to check if the student has passed or not, use if/elif:

if media < 30:
    print('aluno reprovado')
elif media <= 60:
    print ('aluno em recuperação')
else:
    print("aluno aprovado")

That is, if the average is less than 30, it is failed. If the average is not less than 30, it does not enter the if and test the condition of elif. If it is less than or equal to 60, you are recovering (and I don’t need to test again media >= 30, because if you didn’t get into if is because it is definitely greater than or equal to 30 - if it were not, would have entered the if).

Finally, if you didn’t enter the if nor in the elif is because it’s definitely over 60 so I don’t even need to test it again.

And in function mediax do not need to create a variable to return right away. If you are not going to do anything else with it, just return the result of the expression directly:

def mediax(x, y):
    return (x + y) / 2

And you don’t need that variable a. Just make a loop and just interrupt it with break if "end" is entered. It would look like this:

def mediax(x, y):
    return (x + y) / 2

while True: # loop infinito
    nome = input("Nome do aluno")
    mat =  int(input("informe sua matricula"))
    ano = int(input("informe o ano"))
    turma = input("informe a turma")
    nota1 = float(input('entre com a primeira nota'))
    nota2 = float(input('entre com a segunda nota'))
    media = mediax(nota1, nota2)
    print(media)
   
    if media < 30:
        print('aluno reprovado')
    elif media <= 60:
        print ('aluno em recuperação')
    else:
        print("aluno aprovado")
    
    if input('para sair digite (fim) para continuar (enter)') == 'fim':
       break # se digitar "fim", sai do while True

Browser other questions tagged

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