Skip to content

Instantly share code, notes, and snippets.

@dnsouzadev
Created March 15, 2022 16:43
Show Gist options
  • Save dnsouzadev/f2a6bbe35081f774d081629d4ff5123c to your computer and use it in GitHub Desktop.
Save dnsouzadev/f2a6bbe35081f774d081629d4ff5123c to your computer and use it in GitHub Desktop.
Calcular equação de segundo grau em Python
from math import sqrt
def delta(a: int, b: int, c: int):
res = (b ** 2) - 4 * a * c
return res
def function(a: int, b: int, raiz_delta: int):
x1 = (-b + raiz_delta) / (2 * a)
x2 = (-b - raiz_delta) / (2 * a)
return [x1, x2]
def main(a: int, b: int, c: int):
if a == 0:
return "A == 0 significa que isso vira uma equação de primeiro grau!"
res_delta = delta(a, b, c)
if 0 > int(res_delta):
return f"O Delta é {res_delta} e não existe raizes reais para Delta negativo!"
raiz_delta = sqrt(res_delta)
result = function(a, b, raiz_delta)
if result[0] == result[1]:
return f'Como Delta é igual 0 a única raiz real é: {round(result[0],2)}'
return f"As raízes são: {round(result[0], 2)}, {round(result[1], 2)}"
if __name__ == "__main__":
print(main(2, 4, 6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment