collect mouse lifts - 'NoneType' object is not iterable
in OpenSesame
The task: Participants shall press a mouse button to get into a starting position. They shall keep the button pressed and keep the mouse at a certain position (finally i want to do that on a touchscreen).
Then a traffic light appears as a starting counter like 5,4,3,2,1,go!
At the moment it does not go on to 4. Why is the for loop not working?
Image = canvas()
Image['startRect'] = Rect(x=0, y=242, w=159, h=159, color='blue', penwidth=10)
def TRIALhalf1(): #1st part of a trial
shuffle (delay)
start = [995,995,995,995,500] #delay[1] instead of 500
my_mouse = Mouse(visible=True)
my_mouse.flush()
Image.show()
while True: #press Mouse and keep it pressed
button, (x, y), time = my_mouse.get_click()
if (x, y) in Image['startRect']:
break
for i in range(5):
t0 = red[i].show() #timer goes on #red is a list of canvas
#if Mouse is lifted start again
my_mouse = Mouse(visible=True) #is this line necesary?
button, (x, y), time_key_up = my_mouse.get_click_release(timeout=500) #start[i] #THIS IS THE PROBLEMATIC LINE
if button != None:
i=0
TRIALhalf1()
break
TypeError: 'NoneType' object is not iterable
What am i doing wrong? Any ideas?
Comments
Hi,
Let's focus on the
(x, y)
part of this line:If a timeout occurs, the second return value will be
None
, and not anx, y
tuple. Trying to assignNone
to thex, y
tuple results in the error. Try to executex, y = None
in the debug window; you'll get the same error.So this should solve the issue:
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
This error means that python is trying to iterate over a None object. With Python, you can only iterate over an object if that object has a value. This is because iterable objects only have a next item which can be accessed if their value is not equal to None. If you try to iterate over a None object, you encounter the TypeError: ‘NoneType’ object is not iterable error.
For example, list.sort() only change the list but return None.
Python's interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the __iter__ method on the None. None has no __iter__ method defined, so Python's virtual machine tells you what it sees: that NoneType has no __iter__ method.
Technically, you can avoid the NoneType exception by checking if a value is equal to None before you iterate over that value.