How to use a variable of an X function in a Y function in Python?

Asked

Viewed 47 times

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.

  • 2

    Use the return. Study about functions here. In function delta_res, for example, just do: return delta. The value returned by the function can be accessed in the call.

  • Thanks for the link, Felipe! return , but I got the answer ' <' not supported between instances of 'Function' and 'int ' . I tried to transform delta_res in an int ( int(delta) ), but I got int() argument must be a string, a bytes-like Object or a number, not 'Function'

  • Any idea what you might have done wrong?

  • 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/

  • What really happened was that I was calling the job delta_res of main , when it should be calling from within the function bhaskara_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.

Show 1 more comment

1 answer

0

I made some changes based on what you commented, to have access to the value that a function handles the best way is to make this function return this value, I made its function delta_res return the delta value and the function bhaskara_res receive one more value in addition to a,b,c (note: in this bhaskara_res function the variable c is never used so it is recommended to remove it) now also receive the delta value. And when calling the function you can first create a variable to receive the delta value and then put this variable in the bhaskara_res function or directly put the delta_res function in the bhaskha_res function, in the place where the delta should be. In case of doubt just speak.

def delta_res (a, b, c):
  delta = b ** 2 - 4 * a * c
  print("\nDelta é igual a", delta,)
  return delta

def bhaskara_res (a, b, c, delta):
  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

  else:
    #bhaskara_res(a, b, c, delta_res(a,b,c))
    #Você pode usar o código acima ou o código abaixo
    delta = delta_res (a, b, c)
    bhaskara_res (a, b, c, delta)

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: ")))
  • If the delta is zero, just do -b / (2 * a) (because delta is zero, square root is also zero, and add that zero with -b makes no difference).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.