How do I convert number to binary just by using the math functions in python?
<p>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? sorry if my explanation is weird. I'm desperate.</p>

<p>Given the number of down votes, Iâll probably be pilloried for helping youâespecially when you havenât posted any code (pro tip:  the people who answer questions on stack have actual lives where they do things with people they care about).  But enough of my hectoring, start with what you have and go from there is usually a good thing.</p>

<p>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</p>

<pre><code>     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
</code></pre>

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

<pre><code>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
</code></pre>

<hr>

<p>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.  But please do be respectful of other postersâ time by giving the question a try before asking for help.  </p>

python binary