简体   繁体   中英

Global variable x is not defined

Now here is my issue, this is my code located below. This program finds the roots of a number of the users choice. My issue is when I run this code I get, NameError: global name 'x' is not defined . It is coming from the main function when the value x is first encountered. I assume the same will happen to all of the other value's so basically what I am wondering is will I have to define these values outside of the ValueGrabber function? Or can I keep it how it is and just change it a bit?

def ValueGraber():
    x = input("What number do you want to find the root of?:  ")
    root = input("Which root do you want to find?:  ")
    epsilon = input("What accuracy do you want the value to be to? e.g 0.01 :  ")
    high = float(x) + 1
    low = 0.0
    ans = (high + low)/2.0
    Perfect = None
    return x, root, epsilon, ans, high, low, Perfect

def RootFinder(x, root, epsilon, ans, high, low):
    while (ans**float(root)-float(x)) > epsilon and ans != float(x):
        if ans**float(root) > float(x):
        ans = high
        else:
        ans = high
            ans = (high + low)/2.0
    return ans

def PerfectChecker(ans, x, root, Perfect):
    if round(ans, 1)**float(root) == x:
        Perfect = True
    else:
        Perfect = False
    return Perfect

def Output(ans, x, root, perfect, epsilon):
    if Perfect == True:
        print("The number {0} has a perfect {1} root of    {2}".format(float(x),float(root),float(ans)))
    else:
        print("The root of the number {0} has a {1} root of {2} to an accuracy of {3}".format(float(x),float(root),float(ans),float(epsilon)))

def main():
    InputData = ValueGraber()
    DataOpp = RootFinder(x, root, epsilon, ans, high, low)
    PCheck = PerfectChecker(ans, x, root, Perfect)
    DataOutput = Output(ans, x, root, Perfect, epsilon)

main()

In main you refer to x but never defined it.

Your ValueGraber() function does return a value x but that name is not then automatically available to the caller.

The names in the main() function are local ; they don't automatically reflect the names returned from the functions in any case. Here is a fixed main function that just happens to use the same names as the functions for returned values:

def main():    
    x, root, epsilon, ans, high, low, PerfecutData = ValueGraber()
    ans = RootFinder(x, root, epsilon, ans, high, low)
    Perfect = PerfectChecker(ans, x, root, Perfect)
    Output(ans, x, root, Perfect, epsilon)

I removed DateOutput ; it'll always be None because Output() doesn't return anything.

You can still use different names in main ; DataOpp is a perfectly valid local name (even though I personally would use lower_case_with_underscores style for local names, always), but you then should not expect ans to exist, you'd use DataOpp everywhere in main instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM