0
I wrote the code below which is a rectangle triangle calculator using def
to define my custom functions.
def pedir_base():
while True:
try:
base = float(input('Digite o valor da base do triângulo retângulo: '))
if base <= 0:
print(' ')
print('A base precisa ser um número maior que zero.')
continue
except:
print(' ')
print('A base precisa ser um número.')
continue
break
def pedir_angulo():
while True:
try:
angulo = float(input('Digite o valor do ângulo da base com a hipotenusa: '))
if angulo < 0 or angulo == 0 or angulo == 90 or angulo > 90:
print(' ')
print('O ângulo precisa estar entre 0 e 90.')
continue
except:
print(' ')
print('O ângulo precisa ser um número.')
continue
break
def cálculos(a, b):
import math
reto = float(90)
hipotenusa = base/ math.cos(math.radians(angulo))
lado = hipotenusa * math.sin(math.radians(angulo))
LxH = 90 - angulo
perímetro = hipotenusa + lado + base
altura = (lado * base)/ hipotenusa
área = (base * lado)/ 2
r_insc = (lado + base - hipotenusa)/ 2
r_circ = hipotenusa/ 2
def print_dos_valores():
print(' ')
print('O valor da base do triângulo é:', round(base, 2), '.')
print('O valor da hipotenusa do triângulo é:', round(hipotenusa, 2), '.')
print('O valor do lado perpendicular à base do triângulo é:', round(lado, 2), '.')
print('Os valores dos ângulos são:', round(reto, 2), ',', round(angulo, 2), ',', round(LxH, 2), '.')
print(' ')
print('EXTRA:')
print('O valor do perímetro é:', round(perímetro, 2), '.')
print('O valor da altura é:', round(altura, 2), '.')
print('O valor da área é:', round(área, 2), '.')
print('O valor do raio do círculo inscrito é:', round(r_insc, 2), '.')
print('O valor do círculo circunscrito é:', round(r_circ, 2), '.')
print(' ')
print(' ')
def final():
if input('Pressione S caso deseje calcular outro triângulo. Caso deseje fechar a aplicação, pressione qualquer outra tecla.') in ('S', 's'):
print(' ')
print(' ')
if not x:
print('Fechando a aplicação.')
return x
while True:
b = pedir_base()
a = pedir_angulo()
c = cálculos(a, b)
print_dos_valores()
if not final():
break
The problem is that, when arriving at the part of the calculations, it does not recognize the variable angulo
created earlier and says that it is of type Nonetype, which will probably also happen with the variable base
when I get to her. I wonder why this is happening and if I’ve given some other nonsense in this code.
def cálculos(a, b):
import math
reto = float(90)
hipotenusa = base/ math.cos(math.radians(angulo)) #Aqui ele não faz a conta e diz que é NoneType
lado = hipotenusa * math.sin(math.radians(angulo))
LxH = 90 - angulo
perímetro = hipotenusa + lado + base
altura = (lado * base)/ hipotenusa
área = (base * lado)/ 2
r_insc = (lado + base - hipotenusa)/ 2
r_circ = hipotenusa/ 2
Its function
calculos
has 2 parametersa
andb
. Should you or shouldn’t you changea
forangulo
andb
forbase
or otherwise exchange referencesangulo
andbase
fora
andb
? I have at least one more problem: your motivationfinal
uses a variablex
without defining it. Study on the scope of variables.– anonimo
I tried to change the names of the variables, but nothing works, I’m already without ideas of how to get around the problem. About x, I just realized what you said and I’ve fixed it, thank you.
– Matheus Esteves
Local Scope: A local variable (created within a function) exists only within the function where it was declared. Local variables are initialized with each new function call. Thus, it is not possible to access its value outside the function where it was declared. So that we can interact with local variables, we pass parameters and return values in functions.
– anonimo
Global Scope: A global variable is declared (created) outside functions and can be accessed by all functions present in the module where it is defined. Global variables can also be accessed by other modules if they import the module where the variable was defined.
– anonimo
See for example your variable
hipotenusa
, she is local to the functioncalculos
and therefore not known within the functionprint_dos_valores
.– anonimo
do not use accents for variables, change
cálculos
forcalculos
perímetro
forpetrimetros
, etc.– Vitor Ceolin
To see if the angle is valid, you can do
if 0 <= angulo <= 90
(if the angle is between 0 and 90)– hkotsubo