2016-10-10 13 views
0

我在两个函数之间传递信息时遇到了一些麻烦。在我的“试用”函数中(见下面的代码),我正在检查按键,并且如果按下'g'或'h'键,则期望退出while循环。 while循环取决于test = 1。在'check_keys'功能中,只要按下'g'或'h'键,测试就会切换到2;然后我会期望退出while循环。按照现在的情况,当我按键时,我可以看到它们正在注册(由于打印语句),但它并未退出while循环。似乎有一个问题让我的主while循环看到'test'已经从我的'check_keys'函数切换到2。我已经尝试将'测试'更改为全局变量,但我仍然遇到同样的问题。很明显,两种功能之间没有传递信息。Python/Psychopy:在函数之间传递信息

window = visual.Window([600,600],fullscr=False,color=("black"), allowGUI=True, monitor='testMonitor', units='deg') 

global test 
def check_keys():  
    allKeys = event.getKeys(keyList = ('g','h','escape')) 
    for thisKey in allKeys: 
     if thisKey == 'g': 
      print "g" 
      test = 2 

     elif thisKey == 'h': 
      print "h" 
      test = 2 

def trial(): 
    global test 
    test = 1 

    while test ==1: 
     check_keys() 

for i in range(1): 
    trial() 

总之;尽管我的条件'test'变量已经改变,为什么我仍然陷入while循环?

干杯, 史蒂夫

回答

1

你就完蛋了,因为你没有宣布试验为您check_keys的全球内()方法。这就是说,你应该做的是有check_keys返回测试的值,然后只是跟踪它的主要:

def trial(): 
    test = 1 

    while test == 1: 
     test = check_keys()