Charles Hixson wrote: > python3 --version > Python 3.5.3 > > Running on Debian stretch > > In this code s is a string parameter > > while (j < k and \ > (s[j].isalnum()) or \ > (s[j] in seps and s[j+1].isalnum()) ): > j = j + 1 > print ("i = {0}, j = {1}, k = {2}, len[s] = {3}". \ > format(i, j, k, len(s) ) ) > > Yields the result: > > i = 25, j = 27, k = 31, len[s] = 32 > i = 25, j = 28, k = 31, len[s] = 32 > i = 25, j = 29, k = 31, len[s] = 32 > i = 25, j = 30, k = 31, len[s] = 32 > i = 25, j = 31, k = 31, len[s] = 32 > Traceback (most recent call last): > File "parse1.py", line 40, in <module> > print (parse1("The gostack distimms the doshes.")) > File "parse1.py", line 21, in parse1 > (s[j] in seps and s[j+1].isalnum()) ): > IndexError: string index out of range > > I hesitate to report this, because I've been wrong so often, but it > looks to me like the last iteration should not have happened since j is > not less than k.
You have made a bit of a mess of the while condition. Removing random space and newlines: (j < k and (s[j].isalnum()) or (s[j] in seps and s[j+1].isalnum())) The structure is a and b or (c and d) # a: j<k # b: (s[j].isalnum()) # c: s[j] in seps # d: s[j+1].isalnum() which is evalued as (a and b) or (c and d) a and b --> False as you suspect, but then (c and d) is evalued, and if c succeeds also the expression d aka s[j+1].isalnum() which fails on accessing s[j+1]. -- https://mail.python.org/mailman/listinfo/python-list