0

I tried writing a basic program that converted binary to decimal. However, it's not working. Where did I go wrong? What am I missing. Thanks for the help in advance.

n=int(raw_input(' '))
while n = 1:
 k = n % 10
 z= 0
 w=0
 w = k * (pow ( 2, z)) + w
 z = z+1
 n/10
print w
asked Feb 6, 2014 at 21:50
5
  • 1
    can you show an example of how it's not working? are you getting an error? if so, what's the traceback? Commented Feb 6, 2014 at 21:51
  • 1
    also, do you know about int(number, base)? int('10010', 2) == 18 Commented Feb 6, 2014 at 21:53
  • It takes a long time to run, no real output when running a small number like 1011 in 5 minutes Commented Feb 6, 2014 at 21:55
  • @mhlester How would I use that to convert a user input? EDIT: GOT IT Commented Feb 6, 2014 at 22:02
  • @user3281550 - He's saying that your whole script can be reduced to just: print int(raw_input(' '), 2) Commented Feb 6, 2014 at 22:03

1 Answer 1

2

First of all, you used = for a comparison test. Instead, I think that you want to use != (not equal):

while n != 1:

= is only used for assignment.


Also, the line:

n/10

does nothing. Instead, it should be:

n /= 10

which is equivalent to:

n = n / 10
answered Feb 6, 2014 at 21:51
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.