I organized my code into functions but I can't return the values. What is wrong? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

I organized my code into functions but I can't return the values. What is wrong?

def jogar(): define_parametros() imprime_mensagem() calcula_imc() def define_parametros(): peso = float(input('Digite seu peso: ')) altura = float(input('Digite sua altura em metros: ')) imc = peso / (altura ** 2) def imprime_mensagem(): print('Seu IMC é {:.1f}'.format(imc)) print('Você está: ', end = '') def calcula_imc(imc): if imc < 18.5: print('Abaixo do peso ideal.') elif imc >= 18.5 and imc <= 25: print('Com o peso ideal.') elif imc >= 25 and imc <= 30: print('Com sobrepeso.') elif imc >= 30 and imc <= 40: print('Em obesidade.') else: print('Em obesidade mórbida.') if(__name__ == "__main__"): jogar()

24th Aug 2022, 11:42 PM
Nícolas Nascimento
Nícolas Nascimento - avatar
8 ответов
+ 2
Hi! and where do you call the jogar function?
25th Aug 2022, 12:08 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
I updated the code, now it should work?
25th Aug 2022, 12:11 AM
Nícolas Nascimento
Nícolas Nascimento - avatar
+ 2
''' Nícolas Nascimento you can avoid using globals if you return the imc calculated from define_parametros() as an argument to imprime_mensagen(imc) and calcula_imc(imc) ''' def jogar(): imc = define_parametros() imprime_mensagem(imc) calcula_imc(imc) def define_parametros(): peso = float(input('Digite seu peso: ')) altura = float(input('Digite sua altura em metros: ')) imc = peso / (altura ** 2) return imc def imprime_mensagem(imc): print('Seu IMC é {:.1f}'.format(imc)) print('Você está: ', end = '') def calcula_imc(imc): if imc < 18.5: print('Abaixo do peso ideal.') elif imc >= 18.5 and imc <= 25: print('Com o peso ideal.') elif imc >= 25 and imc <= 30: print('Com sobrepeso.') elif imc >= 30 and imc <= 40: print('Em obesidade.') else: print('Em obesidade mórbida.') if(__name__ == "__main__"): jogar()
25th Aug 2022, 4:20 AM
Bob_Li
Bob_Li - avatar
+ 1
First, you are trying to use the global variable imc, but you are not accessing it as a global variable, that is, in each function you have different variables with the same name imc. Second, you created the calcula_imc function with an argument, but you call it without an argument. Debug: imc = 0.0 def jogar(): define_parametros() imprime_mensagem() calcula_imc(imc) def define_parametros(): peso = float(input('Digite seu peso: ')) altura = float(input('Digite sua altura em metros: ')) global imc imc = peso / (altura ** 2) And thirdly, the use of global variables is considered not a very good practice, I would get rid of it.
25th Aug 2022, 1:33 AM
Solo
Solo - avatar
0
probably... can be code run even without calling this function, but it is difficult to check in the sololearn, since you are in the pop-up window at the very beginning when you pressed the run button, and the input window popped up you must enter all the required values at once
25th Aug 2022, 12:15 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
and you dont have to come up with complex constructs with if. simply write the name of the function to call it and empty brackets (if the function does not require additional parameters)
25th Aug 2022, 12:19 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
#try to run this code num1=int(input('enter number 1: \n')) num2=int(input('enter number 2: \n')) total=num1+num2 print(total)
25th Aug 2022, 12:26 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Thank you so much guys!!!! <3
9th Sep 2022, 4:25 PM
Nícolas Nascimento
Nícolas Nascimento - avatar