Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reference a variable outside of function in python

I am trying to reference variables root1 and root 2 in the function Print(numA, numB, numC, root1, root2, EquationKind), however, I do not want to use global variables.

I get the error message:

Line 54: NameError: name ‘root1’ is not defined

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my code:

import math

def main():
    while True:
        def GetInputs():
            numA = int(input("What is coefficient A?"))
            numB = int(input("What is coefficient B?"))
            numC = int(input("What is coefficient C?"))
            return numA, numB, numC

        def QuadRoots(numA, numB, numC):
            if numA == 0:
                return -1
        
            else:
                discriminant = numB ** 2 - (4 * (numA * numC))
        
                if discriminant < 0:
                    return -2
            
                elif discriminant > 0:
                    numnegB = numB * -1
                    plussolution = numnegB + math.sqrt(numB ** 2 - (4 * (numA * numC))) 
                    minussolution =  numnegB - math.sqrt(numB ** 2 - (4 * (numA * numC)))
                    root1 = plussolution / (2 * numA)  
                    root2 = minussolution / (2 * numA)  
                    return 0, root1, root2
                
                elif discriminant == 0:
                    numnegB = numB * -1
                    root1 = numnegB / (2 * numA)
                    return 1, root1
                
        numA, numB, numC = GetInputs()
        EquationKind = QuadRoots(numA, numB, numC)
    
        def Print(numA, numB, numC, root1, root2, EquationKind):
            if EquationKind == -1:
                print("This is a line; not a Quadratic Equation.")
                
            elif EquationKind == -2:
                print(str(numB) + "x^2" + " + " + str(numA) + "x" + " + " + str(numC) + " has no  solution.") 
            
            elif EquationKind == 0:
                print(str(numB) + "x^2" + " + " + str(numA) + "x" + " + " + str(numC) + " has solutions " + str(root1) + " and " + str(root2) + ".")
            
            elif EquationKind == 1:
                print(str(numB) + "x^2" + " + " + str(numA) + "x" + " + " + str(numC) + " has unique solution " + str(root1) + ".")
                                        
         
        Print(numA, numB, numC, root1, root2, EquationKind)

        askuser = input("Do you want to solve another equation? Type Y for yes and N for no:")
        
        if askuser == "N" or askuser == "n":
            print("Goodbye!")
            break
            
        elif askuser == "Y" or askuser == "y":
            continue
        
        else:
            print("Please type Y or N")
            break
            
main()

The goal of the program is to solve quadratic equations, it should test to see if the equation has a solution or not.

For example, if the user entered numbers 1, 3, and 2, the output should be 3x^2 + 1x + 2 has solutions -1.0 and -2.0.

>Solution :

The problem is that in line 38 you are assigning the value to the variable EquationKind regardless of whether you return 1, 2 or 3 values. So variables root1, root2 only exist during the execution of the QuadRoots method.

If you look at the variables numA, numB, numC you have declared them out of the method.

One option you can use (although I think not the most optimal) is to add:
EquationKind, root1, root2 = QuadRoots(numA, numB, numC) and in the Quadroots method always return 3 values.

I hope I have helped you

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading