现在这是我的问题,这是我的代码位于下面。这个程序找到了一些用户选择的根源。我的问题是,当我运行此代码时,我得到NameError: global name 'x' is not defined
. 它来自第一次遇到main
值时的函数。x
我假设所有其他值都会发生同样的情况,所以基本上我想知道的是我是否必须在ValueGrabber
函数之外定义这些值?还是我可以保持原样并稍微改变一下?
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()