I have only recently begun programming in Python (with previous Ruby experience). I am trying to set up an if condition with two conditions:
if not previous[1] and previous[0][0] == 5:
prin开发者_JS百科t "hello world"
However, I keep getting this error:
<type 'exceptions.IndexError'>: tuple index out of range
Print previous returns: ((5, 1, 9, 23),)
What am I doing wrong?
I am looking for something similar to they Ruby Syntax: unless previous[1]
((5, 1, 9, 23),)
, then this is a length-1 tuple. It's only element--with index 0--is the tuple (5, 1, 9, 23)
. It doesn't have a second element to have the index 1
, so that_tuple[1]
raises IndexError
.
What did you hope previous[1]
would give you?
There is only a single element in 'previous
'. That is why you are getting an IndexError
when you attempt to retrieve element 1.
Your index error is coming from previous[1]
- previous is 1-tuple ... you'd need previous[0]
and previous[0][0]
.
精彩评论