求教各位大哥,我是一个心理学学生,用的软件是psychopy,psychopy很少用代码,基本都是插入按键,有一些复杂的地方需要插入代码,我没写过代码,这段代码是复制的一个实验的psychopy的代码(和我的实验任务一样,他的是荷兰语所以有些字母不同),主要任务就是输入图片对应的英文单词。
n= len(theseKeys)
i = 0
while i < n:
if theseKeys[i] == 'return':
# pressing RETURN means time to stop
continueRoutine = False
break
elif theseKeys[i] == 'backspace':
inputText = inputText[:-1] # lose the final character
i = i + 1
elif theseKeys[i] == 'space':
inputText += ' '
i = i + 1
elif theseKeys[i] in ['lshift', 'rshift']:
shift_flag = True
i = i + 1
elif theseKeys[i] in ['ralt', 'roption', 'lalt', 'loption']:
alt_flag = True
i += 1
else:
if len(theseKeys[i]) == 1:
# we only have 1 char so should be a normal key,
# otherwise it might be 'ctrl' or similar so ignore it
if shift_flag:
inputText += chr( ord(theseKeys[i]) - ord(' '))
shift_flag = False
elif alt_flag:
pl_map = { 'a':u'ą', 'e':u'ę' , 'o':u'ó', 'l':u'ł', 's':u'ś', 'c':u'ć', 'z':u'ż', 'x':u'ź' }
if theseKeys[i] in pl_map.keys():
theseKeys[i] = pl_map[theseKeys[i]]
inputText += theseKeys[i]
alt_flag = False
else:
inputText += theseKeys[i]
i = i + 1
报错的是这一行。TypeError: object of type 'KeyPress' has no len()
if len(theseKeys[i]) == 1:
请问一下我应该怎么改呢?
谢谢大佬。
theseKeys[i] 是一些字符串, ‘ralt', 'lalt' 之类的, len(theseKeys[i]) 应该是返回字符串的长度。
你把这句话放到程序开始看看有什么输出:
print(theseKeys[i])