2

I need a code in Python 3.3 to convert an integer into binary.This is my first try:

a = input(str("Please Enter a Number")if a == float:    print (1)else print(0)b = a/2while True:    if b == float:        print(1)    else print(0)

I don't know why I keep getting errors with theif a == float:.And I know that the rest of the code is wrong too, but this: makes me crazy.

askedOct 29, 2013 at 20:48
John Smith88's user avatar
8
  • What are you trying to accomplish with "if a == float:"?CommentedOct 29, 2013 at 20:50
  • What is float in this case?CommentedOct 29, 2013 at 20:51
  • Also, what are you trying to accomplish with your while loop?CommentedOct 29, 2013 at 20:52
  • 1
    John, welcome to Stack Overflow. I notice that your post does not include a question. Maybe you are asking "How do I convert integer to binary?" Or maybe you are asking "why doesn'ta == float do what I want?" Since you didn't ask a question, it is hard to know which question to answer. Please edit your post to include a specific question.CommentedOct 29, 2013 at 20:52
  • 1
    Nah, I try to complain about only one thing at a time, particularly with new members. I haven't figured out how to say "ENOQUESTION" and "SSCCE.ORG" in one breath.CommentedOct 29, 2013 at 20:54

4 Answers4

11

Your code has a lot of issues:

  1. Your indentation is off. Indentation is very important in Python since that is how it knows what goes with what.
  2. You need to useisinstance to see if an object is a float. I assume this is what you are trying to do witha == float. But, that doesn't make sense because, in Python 3.x.,input always returns a string object. So,a is a string. However, iffloat is actually a variable, then you should change its name. Naming a variablefloat is a bad practice since it overrides the built-in.
  3. You are missing a colon at the end of eachelse.
  4. You are missing a closing parenthesis on the first line.
  5. Thestr in the first line is unnecessary (not an error, but I just thought I'd mention it).

However, instead of fixing all this, I'm going to introduce you to thebin built-in:

>>> n = 127>>> bin(n)>>> # The "0b" at the start means "binary".'0b1111111'>>> # This gets rid of the "0b">>> bin(n)[2:]'1111111'>>>

It was built explicitly to do what you are trying to do.

Also, here are some references on Python you might enjoy:

http://www.tutorialspoint.com/python/python_overview.htm

http://wiki.python.org/moin/BeginnersGuide/Programmers

answeredOct 29, 2013 at 20:49
Sign up to request clarification or add additional context in comments.

3 Comments

Not to mention there's a missing closing parenthesis on the first line.
I'm not convinced thata == float is a type test. He might actually have a variable namedfloat.
Yea, I don't know precisely either. Let me amend my post.
1

You can just use thebin function:

>>> bin(100)'0b1100100'

Ignore the0b infront of the string. You can always get the raw binary numbers using usingbin(your_numer)[2:].

Also, you can get this using theformat function:

>>> format(100, 'b')'1100100'
answeredOct 29, 2013 at 20:50
Nafiul Islam's user avatar

Comments

0

If you need to print it in binary you can just do: print(bin(a))

answeredOct 29, 2013 at 20:51
Nighthawk's user avatar

Comments

0

This is what i made

while True:    print("FIND OUT WHAT BINARY THIS IS")    space = " "    num1 = int(input())    while num1 > 0:        if num1 % 2 == 0:          space = space + "0"        else:          space = space + "1"        num1 = int(num1 / 2)    else:     space = space[::-1]     print(space)
answeredApr 6, 2017 at 17:21
ShittyBoy's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.