-7

My assignment is to make a code to change a number between 0 and 255 to binary. All we have learned is the print, input, and the math functions? How would I code it so that when I have it ask for a number I can type the number in and it will go through the process of converting it?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 6
    Where's your code so far? This question is too vague (and too much "do my homework for me") for Stack Overflow. – user94559 Oct 18 '16 at 21:16
  • 1
    Google is your friend. Try a quick search for "convert number to binary python" – MichaelMaggs Oct 18 '16 at 21:20
  • Sorry for being vague... But I've tried googling and even tried this website. I know how to convert a decimal to binary, but I don't know how to code it on python. The only thing iI've found on the internet is Convert_to_binary and that doesn't actually work. I'm trying to figure out after I do num=float(input("Enter a number between 0 and 255: ")) how do I make a code going through the process of checking if num-2**8 goes into my number (obviously it doesn't) and so on and so fourth with the rest of the exponents. – Peyton Elli Lewis Oct 18 '16 at 21:33
  • 1
    Have you tried the idea suggested in the very first result that comes up in the Google search? http://stackoverflow.com/questions/10411085/converting-integer-to-binary-in-python – MichaelMaggs Oct 18 '16 at 21:40
  • 1
    Or the second result? http://stackoverflow.com/questions/699866/python-int-to-binary – MichaelMaggs Oct 18 '16 at 21:41
  • 1
    We aren't allowed to use the easy route and just use bin(). We have to use loops, inputs, and floats to code a way to convert it without using bin() the whole point of the assignment is that whenever I type a number in my code will make take it through the process of seeing if 2^8, 2^7, 2^6... go into it until you reach 0. I know how to make inputs and everything, but I don't know what to do when say 147 is subtracted by 2^8 and it's a negative. – Peyton Elli Lewis Oct 18 '16 at 21:46
  • use dividing by 2. – furas Oct 18 '16 at 21:57
  • Both Google results showed you a quick way of doing what you want using only the `print` function (not `bin()`), and you said in your question you could use `print`. Do bear in mind that it's very difficult for people to help you unless you provide specific details of what you require and what you have already tried. – MichaelMaggs Oct 19 '16 at 05:17

1 Answers1

2

First off, 2^8 = 256, but your input range is 0 => 255. So you don’t have to worry about 2^8. Now, for base conversion, we usually work left to right like this

     128   64  32  16   8   4   2   1
147   1    0   0   1    0   0   1   1
     147   19  19  3    3   3   1   0

Running totals along the bottom row and converting 147 from decimal yields 10010011 in binary. And now you can replicate the same using code. As a starting point, you probably want something along the following lines

user_input = . . . # get the input from the user
running_total = user_input
for exponent in range(7, -1, -1):
    # if my running total is greater than 2**exponent
    # subtract 2**exponent from the running total and
    # print 1 else print 0
    pass

n.b. there are all sorts of fancy ways to do this, and base conversion is a lot of fun when you get into it.

halfer
  • 19,824
  • 17
  • 99
  • 186
2ps
  • 15,099
  • 2
  • 27
  • 47