如何使用来自两个函数的 return 值来制作 if 语句和中断过程
How to use return values from two functions to make an if statement and break process
我正在尝试 运行 一个程序,如果我的值在阈值之内,我的状态 1 和 2 就会变为正值。我希望它是这样的,当两个状态都为正时,程序就会中断。但是,我无法使用我所在州的值。
我该如何处理这个问题?
import concurrent.futures
def Vibration1(a,b,c,d):
State1 = False
if a < Thumb_Pitch < b or c < Roll_Max_Thumb < d :
State1 = True
else:
State1 = False
print('Done1')
return State1
def Vibration2(w,x,y,z):
State2 = False
if w < Index_Pitch < x or y < Index_Roll < z :
State2 = True
else:
State2 = False
print('Done2')
return State2
def feedback():
Success = False
if State1 and State2 == True:
Success = True
x = time.sleep(5)
else:
Success = False
print("Keep Trying")
return print('Success',Success)
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(Vibration1,-3000.0,3000.0,-3000.0,3000.0)
f2 = executor.submit(Vibration2,-3000.0,3000.0,-3000.0,3000.00)
f3 = executor.submit(feedback)
print(f1.result())
print(f2.result())
print(f3.result())
要解决这个问题,您不能从函数 Vibration1
和 Vibration2
中引用变量。
有两种方法可以做到这一点。
1) 您可以将值赋给全局变量:
import concurrent.futures
State1 = None
State2 = None
def Vibration1(a,b,c,d):
global State1
State1 = False
if a < Thumb_Pitch < b or c < Roll_Max_Thumb < d :
State1 = True
else:
State1 = False
print('Done1')
return State1
def Vibration2(w,x,y,z):
global State2
State2 = False
if w < Index_Pitch < x or y < Index_Roll < z :
State2 = True
else:
State2 = False
print('Done2')
return State2
def feedback(a,b,c,d,w,x,y,z):
global State1
global State2
Success = False
if State1 and State2:
Success = True
x = time.sleep(5)
State1 = False
State2 = False
else:
Success = False
print("Keep Trying")
return print('Success',Success)
如果这样做,您将不得不在外部调用 Vibration1
和 Vibration2
函数,以便为变量赋值。
2) 当你引用值 State1 和 State2 时你调用振动函数:
def feedback(a,b,c,d,w,x,y,z):
Success = False
if Vibration1(a,b,c,d) and Vibration2(w,x,y,z):
Success = True
x = time.sleep(5)
else:
Success = False
print("Keep Trying")
return print('Success',Success)
这样你就不用调用外部函数了,它们会被自动调用。
希望对您有所帮助!如果您有任何问题,请告诉我。
全局变量并不是很好的做法,但简单的解决方案是将 State1
和 State2
声明为全局变量。
你通过添加行来做到这一点
global State1
global State2
在每个要修改的函数的开头 State1
或 State2
我正在尝试 运行 一个程序,如果我的值在阈值之内,我的状态 1 和 2 就会变为正值。我希望它是这样的,当两个状态都为正时,程序就会中断。但是,我无法使用我所在州的值。
我该如何处理这个问题?
import concurrent.futures
def Vibration1(a,b,c,d):
State1 = False
if a < Thumb_Pitch < b or c < Roll_Max_Thumb < d :
State1 = True
else:
State1 = False
print('Done1')
return State1
def Vibration2(w,x,y,z):
State2 = False
if w < Index_Pitch < x or y < Index_Roll < z :
State2 = True
else:
State2 = False
print('Done2')
return State2
def feedback():
Success = False
if State1 and State2 == True:
Success = True
x = time.sleep(5)
else:
Success = False
print("Keep Trying")
return print('Success',Success)
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(Vibration1,-3000.0,3000.0,-3000.0,3000.0)
f2 = executor.submit(Vibration2,-3000.0,3000.0,-3000.0,3000.00)
f3 = executor.submit(feedback)
print(f1.result())
print(f2.result())
print(f3.result())
要解决这个问题,您不能从函数 Vibration1
和 Vibration2
中引用变量。
有两种方法可以做到这一点。
1) 您可以将值赋给全局变量:
import concurrent.futures
State1 = None
State2 = None
def Vibration1(a,b,c,d):
global State1
State1 = False
if a < Thumb_Pitch < b or c < Roll_Max_Thumb < d :
State1 = True
else:
State1 = False
print('Done1')
return State1
def Vibration2(w,x,y,z):
global State2
State2 = False
if w < Index_Pitch < x or y < Index_Roll < z :
State2 = True
else:
State2 = False
print('Done2')
return State2
def feedback(a,b,c,d,w,x,y,z):
global State1
global State2
Success = False
if State1 and State2:
Success = True
x = time.sleep(5)
State1 = False
State2 = False
else:
Success = False
print("Keep Trying")
return print('Success',Success)
如果这样做,您将不得不在外部调用 Vibration1
和 Vibration2
函数,以便为变量赋值。
2) 当你引用值 State1 和 State2 时你调用振动函数:
def feedback(a,b,c,d,w,x,y,z):
Success = False
if Vibration1(a,b,c,d) and Vibration2(w,x,y,z):
Success = True
x = time.sleep(5)
else:
Success = False
print("Keep Trying")
return print('Success',Success)
这样你就不用调用外部函数了,它们会被自动调用。
希望对您有所帮助!如果您有任何问题,请告诉我。
全局变量并不是很好的做法,但简单的解决方案是将 State1
和 State2
声明为全局变量。
你通过添加行来做到这一点
global State1
global State2
在每个要修改的函数的开头 State1
或 State2