Question: Given a base-**10** integer,**n** , convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1's in **n**'s binary representation.
Here is the code I wrote for it

```
#!/bin/python
import sys
k=0
m=0
i=0
n = int(raw_input().strip())
x=bin(n)[2:]
while x[i] :
    if x[i]==1:
        m=m+1
    else :
        if k<m:
            k=m
            m=0
    i+=1
print(k)

```

I am getting the following error 

```
Traceback (most recent call last):
File "solution.py", line 9, in <module>
while x[i] :
 IndexError: string index out of range

```

Answer: 
Your while loop increments `i` but never checks when it has gone past the end of the string `x`. Your test `while x[i]` does not check for the string's end, it just checks each character if is empty. Since no character in a string is empty (and your string contains only `'0'` and `'1'`), your loop just continues. Note that the string `'0'` does not test to `False`: it is a perfectly valid string and evaluates to `True`.

If you want that structure, instead use the check

```
while i < len(x):

```

but it would be more pythonic to restructure your code and use

```
for c in x:

```

which looks at each character `c` in string `x`.
Your condition `while x[i]` is wrong since it will never evaluate to `False` for any character. Are you used to C's null-terminated strings?

Also, you are comparing a `str` to an `int` here:

```
if x[i]==1:

```

The proper way to do this would be to use a for-each loop:

```
import sys

k = 0
m = 0
i = 0

n = int(raw_input().strip())
x = bin(n)[2:]

for c in x:
    if c == '1':
        m += 1
    else:
        if k < m:
            k = m
        m = 0

# check whether the last run is greater than the current maximum
if k < m:
    k = m

print(k)

```