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
is simple, as says error "i" and "j" are not defined, should be
media = mediax(nota1,nota2)
, which are the variables that are defined– Ricardo Pontual