what is wrong with my code in my continous 1's python program?
<p>so i am a newbie so be easy on me i was just trying to solve a problem in hackerrank.com
in which i must get the max number of continous 1's in a binary representation of number and i made it but it is not working Heres the Code</p>

<p>the updated code is below</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>import statistics
# Get the input using  the variable Getinput
Getinput = int(input())
# def a function names 1 seq
def one_seq(n):
    main = []
    # get the binary of the number taken into input
    # convert the binary number to a string
    number = "{0:b}".format(n)
    # i = 0
    i = 0
    # sub_main =[]
    sub_main = []
        #a while loop 
    while i != len(number)-1:
            #if number[i] == "1":
            if number[i] == "1":
                sub_main.append(number[i])
            #else definately 0 so append sub_main to main and sub_main = []
            else:
                main.append(sub_main)
                sub_main = []
            i+=1
    # greatest 1 = len(x) for element in main
    #greatest_one = [len(element) for element in main ]
    greatest_one = [len(element) for element in main]
    #return greatest_one
    return max(greatest_one)


print(len(one_seq(Getinput)))</code></pre>
</div>
</div>
</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>import statistics
# Get the input using  the variable Getinput
# def a function names 1 seq
def one_seq(n):
    main = []
    # get the binary of the number taken into input
    # convert the binary number to a string
    number = str("{0:b}".format(n))
    # i = 0
    i = 0
    # sub_main =[]
    sub_main = []
        #a while loop set to True
    while i != len(number):
            #if number[i] == 1:
            if number[i] == 1:
                sub_main.append(number[i])
            #else definately 0 so append sub_main to main and sub_main = []
            else:
                main.append(sub_main)
                sub_main = []
                continue
            i+=1
    # greatest 1 = len(x) for element in main
    greatest_one = [len(element) for element in main ]
    return statistics.mode(greatest_one)


one_seq(2)
                </code></pre>
</div>
</div>
</p>

<p>You're close. I'll annotate your mistakes to point you in the right direction,</p>

<pre><code>import statistics

def one_seq(n):
    main = []
    number = str("{0:b}".format(n))
    i = 0
    sub_main = []
    while i != len(number):
            if number[i] == 1: # you want to check against the string '1' not the number 1
                sub_main.append(number[i])
            else:
                main.append(sub_main)
                sub_main = []
                continue # this will skip the i+=1 and cause an infinite loop
            i+=1
    greatest_one = [len(element) for element in main ]
    return statistics.mode(greatest_one)  # There's no reason to use mode here.
                                          # You want the greatest element in the list
                                          # Not the most common.
</code></pre>

<p>Remember if you get stuck you can always insert prints everywhere.</p>

<h3>Ints and strings</h3>

<pre><code>number = 11  # binary 1100101
binary = str("{0:b}".format(number))
print(binary[0] == 1) # prints False
print(binary[0] == '1') # prints True
</code></pre>

<p>The important thing to keep in mind is that the variable binary is a <code>str</code>. In Python a string is written 'hello' or '0', while an int is written 0 or 1 or 3562. When an int is compared to a string using == it will always return False.</p>

<h3>The meaning of continue</h3>

<pre><code>while True:
    print('This be printed over and over')
    continue
    print('This will never be printed')
</code></pre>

<p>Continue skips the rest of an iteration of a loop.</p>

<h3>Finding the max of a list</h3>

<pre><code>mylist = [8, 2, 4, 68, 7]
largest = max(mylist)
print(largest) # prints 68
</code></pre>

<h1>The solution</h1>

<pre><code>def one_seq(n):
    main = []
    number = str("{0:b}".format(n))
    print(number)
    i = 0
    sub_main = []
    while i != len(number):
            if number[i] == '1': # Changed 1 to '1'
                sub_main.append(number[i])
            else:
                main.append(sub_main)
                sub_main = []
                # Removed continue
            i+=1
    main.append(sub_main) # This is something I missed earlier
                          # this fixes an issue with numbers
                          # ending with 1s.
    greatest_one = [len(element) for element in main ]
    return max(greatest_one) # Used max instead of mode
</code></pre>

<h1>Simplified</h1>

<p>I would probably write it like this</p>

<pre><code>def one_seq(n):
    current = 0
    longest = 0
    for digit in '{0:b}'.format(n):
        if digit == '1':
            current += 1
        else:
            longest = max(current, longest)
            current = 0
    longest = max(current, longest)
    return longest
</code></pre>

python