-2
HELLO, I’m trying to resolve the issue below, but I can’t make the LOOP work, since the exercise asks that even after choosing a type of average, the instruction comes back asking infinitely the same thing:
***Implement a python script with:
- a function that takes three numerical values and one character per parameter .
- if the character passed as argument is 'A' or 'a', the function returns the arithmetic mean of the numerical values
- if the character passed as argument is 'P' or 'p', the function returns the weighted average (weights: 5, 3 and 2) of the numerical values;
- and if the character passed as argument is 'H' or 'h', the function returns the harmonic mean of the numerical values.
- this script should interact with the user referring to the three values and also what type of average the user wants to calculate.
- this script must be interactive, that is, it must query if the user wants to perform a new calculation at the end of each execution and, if the answer is affirmative, repeat the process.***
My code for the above question:
num1=float(input('Digite um primeiro número: '))
num2=float(input('Digite um segundo número: '))
num3=float(input('Digite um terceiro número: '))
#FORMULAS DAS MEDIAS:
med_arit=((num1 + num2 + num3) / 3)
med_harm=((1/num1 + 1/num2 + 1/num3)/3)
med_pond=((num1*5 + num2*3 + num3*2) / 10)
print('Os números inseridos respectivamente são: {:.0f}, {:.0f} e {:.0f}.'.format(num1,num2,num3)) print('================================', '\n')
med=str(input('Agora para saber as MÉDIAS:
\nARITMÉTICA, digite [A];
\nHARMÔNICA digite [H]'
\nPONDERADA digite [P]'
\nDIGITE A LETRA DA MÉDIA DESEJADA: '))
while True:
try:
if med in 'Aa':
print('A MÉDIA ARITMÉTICA é igual a ', med_arit)
break
elif med in "Hh":
print('A MÉDIA HARMÔNICA é igual a ', med_harm)
break
elif med in "Pp":
print('A MÉDIA PONDERADA é igual a ',med_pond)
break
else:
print(input('Voçê inseriu um valor errado, tente novamente: '))