how to make if conditions work with specific string characters?
<p>I want to develop a program that takes input in the form of bits and outputs the decimal equivalent. </p>

<p>I am restricted in the kind of functions I can use so this is what I came up with:</p>

<pre><code>digits=raw_input("enter 1's and 0's")
a=len(digits)
repeat=a
c=0
total=0
while (c &lt; repeat):
    if digits[c]==1:
        total=total + (2**a)
        c=c+1
        a=a-1
    else:
        c=c+1
        a=a-1
print total
</code></pre>

<p>The code returns <code>0</code> for all kinds of strings I enter. </p>

<p>How can I fix my <code>if</code> statement to work correctly.</p>

<p>The reason its not working is that the variable digits is string and when you compare it with integer one it won't work you have to convert it to the below</p>

<pre><code>if int(digits[c]) == 1:
</code></pre>

<p>or </p>

<pre><code>if str(digits[c]) == "1":
</code></pre>

<p>Also your code has a lot of unwanted lines, so please find below the smaller and working code for the same</p>

<pre><code>digits=raw_input("enter 1's and 0's")
a=len(digits)
c = 0
total = 0
while (c &lt; a):
    if int(digits[c])==1:
        total += pow(2,c)
    c += 1    
print "converted value : ", total
</code></pre>

<p>There is better of way of writing it using for loop than while, please find below the code fior the saem using for loop </p>

<pre><code>digits=raw_input("enter 1's and 0's")
total = 0
for c in xrange(len(digits)):
    if int(digits[c])==1:
        total += pow(2,c)   
print total
</code></pre>

<p>Even better way of doing it in 2 lines below</p>

<pre><code>binary_no = raw_input("Enter binary no : ")
# It converts the string to base 2 decimal equivalent
decimal_eqivalent = int(binary_no, 2)

print "Decimal eq : ", decimal_eqivalent
</code></pre>

python