0
I am a student of the first period of Eng. of Computing I’m having some problems to understand how I could (if possible) use the variable of one function within another, the two being executed within a third function.
The problem is simple (and I have already managed to make it work more rustic), I have to create a basic program to calculate a Bhaskara formula and inform delta, roots, etc. However I would like to create a function to deal with delta, one to deal with the roots and everything within a Main function.
'''
def delta_res (a, b, c):
delta = b ** 2 - 4 * a * c
print("\nDelta é igual a", delta,)
def bhaskara_res (a, b, c):
if delta < 0:
print("\nA equação não tem raízes.\n")
elif delta == 0:
print("\nA equação tem apenas uma raíz.\n")
x1 = (-b + delta ** 0.5) / (2 * a)
else:
print("\nA equação tem duas raízes.\n")
x1 = (b + delta ** 0.5) / (2 * a)
x2 = (b - delta ** 0.5) / (2 * a)
print("As raízes da equação são",x1 , "e", x2,"\n")
def main (a, b, c):
if a == 0:
print("\nSe 'a' é igual a 0, a equação não é de segundo grau.\n")
return
delta_res (a, b, c)
bhaskara_res (a, b, c)
main(a=int(input("Dê um valor para a: ")),
b=int(input("Dê um valor para b: ")),
c=int(input("Dê um valor para c: ")))
'''
However, the variable "delta" within the function "bhaskara_res" cannot access the variable "delta" within "delta_res".
How would I solve this problem?
Please edit the question to limit it to a specific problem with sufficient detail to identify an appropriate answer.
–
Use the
return
. Study about functions here. In functiondelta_res
, for example, just do:return delta
. The value returned by the function can be accessed in the call.– Luiz Felipe
Thanks for the link, Felipe!
return
, but I got the answer ' <' not supported between instances of 'Function' and 'int ' . I tried to transformdelta_res
in an int (int(delta)
), but I got int() argument must be a string, a bytes-like Object or a number, not 'Function'– Gabriel Vieira
Any idea what you might have done wrong?
– Gabriel Vieira
You need to call the function... The document I mentioned shows how to do this, did you ever read? Another link: https://www.freecodecamp.org/news/functions-in-python-a-beginners-guide/
– Luiz Felipe
What really happened was that I was calling the job
delta_res
ofmain
, when it should be calling from within the functionbhaskara_res
. Simple thing. I thought about erasing question by the same judgment, but I think it can be useful for someone else without experience like me who needs.– Gabriel Vieira