Теперь вот моя проблема, это мой код, расположенный ниже. Эта программа находит корни множества пользователей. Моя проблема в том, когда я запускаю этот код, который я получаю, NameError: global name 'x' is not defined
. Он исходит из функции main
, когда впервые встречается значение x
. Я предполагаю, что то же самое произойдет со всем другим значением, так что в основном то, что мне интересно, будет ли я определять эти значения вне функции ValueGrabber
? Или я могу сохранить его, как он есть, и немного изменить его?Глобальная переменная x не определена
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()