所以我想要完成的是通过使用 counter + 1 检查一个元素是否为空,但我一直让索引超出范围,这本质上意味着下一个元素不存在,但是我希望程序返回而不是抛出异常我的 if 语句的布尔值是可能的..?本质上,我实际上想查看字典中元组的下一个元素,看看它是否为空。
>>> counter = 1
>>> list = 1,2,3,4
>>> print list
>>> (1, 23, 34, 46)
>>> >>> list[counter]
23
>>> list[counter + 1]
34
>>> list[counter + 2]
46
>>> if list[counter + 3]:
... print hello
... else:
... print bye
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range