Python lazy boolean evaluation gone wrong?
Why this works:
s = 'xyz'
i = 0
while i < len(s) and s[i] not in 'aeiou':
print(s[i])
i += 1
x
y
z
... but this does not?
s = 'xyz'
i = 0
while s[i] not in 'aeiou' and i < len(s):
print(s[i])
i += 1
x
y
z
Traceback (most recent call last):
File "<pyshell#135>", line 1, in <module>
while s[i] not in 'aeiou' and i <= len(s):
IndexError: string index out of range
I'm confused, what am I missing here?
No comments:
Post a Comment