Custard Jesus
Custard Jesus

Reputation: 5

Quadratic Formula Calculator

Could someone please help me figure out where I went wrong with this very basic code I wrote this morning? Trying to write a calculator for the quadratic formula question on Python 3 but I get an error saying "NameError: name 'sqrt' is not defined". It sort of makes sense, but then I do not know how else to put the square root in there. Is there some other function I should be using?

PS: I learn on my own off youtube and ebooks. So if you could please explain to me like I'm five, that would be awesome, thanks. I just started learning a couple days ago.


    # Quadratic Formula Calculator
    # What could possibly go wrong?
    
    # Define a, b, and c variables for the quad formula
    a = int(input("What is your 'a' variable? "))
    b = int(input("What is your 'b' variable? "))
    c = int(input("What is your 'c' variable? "))
    
    print(f"""
    Your quadratic formula result in + is...
    {((-b) + ((sqrt(b) ** 2) - (4 * a * c))) / (2 * a)}
    """)
    
    print(f"""
    Your quadratic formula result in - is...
    {((-b) - ((sqrt(b) ** 2) - (4 * a * c))) / (2 * a)}
    """)

Upvotes: 0

Views: 204

Answers (2)

Shuvam Paul
Shuvam Paul

Reputation: 138

sqrt is not a built-in function of python. It is a function of the math module. So you must first import math.

The code must be:

    import math
    a = int(input("What is your 'a' variable? "))
    b = int(input("What is your 'b' variable? "))
    c = int(input("What is your 'c' variable? "))
        
    print(f"""
    Your quadratic formula result in + is...
    {((-b) +  (math.sqrt(b ** 2 - 4 * a * c))) / (2 * a)}
     """)
        
    print(f"""
    Your quadratic formula result in - is...
    {((-b) - (math.sqrt(b ** 2 - 4 * a * c))) / (2 * a)}
    """)

Upvotes: 2

con
con

Reputation: 6093

You need to load the sqrt function thus:

import math

and then use your square root like this:

math.sqrt(9)

which returns 3.

Upvotes: 1

Related Questions