Python Decimal to Binary converter (without using bin() or output formatting) There is no input validation for this, but that aside, I would appreciate it if anyone could help me improve the logic of this script.

import math
#Input
num = input("Enter a whole number: ")

#Floors input in case of a non whole number
decimal = math.floor(float(num))

#Variables
exp = 0
divTwo = 1
zeros = 0
total = ""
if decimal &lt; 0:
    total += str('-')
    decimal = -decimal
if decimal == 0:
    total = str('0')
else:
    #Loops while remainder is &gt; 0.
    while int(decimal) &gt; 0:

        #Increases exponent by one if 2^exp is less than or equal to the remainder.
        if 2 ** exp &lt;= int(decimal):
                exp += 1

        else: #Multiplies 2 by the exponent and places zeros in between 1s. Also subtracts from the exponent.
                exp -= 1

                #Determines whether or not to subtract from the remainder and add a '1' to the binary translation or just add a '0' to the binary translation.
                if 2 ** exp &lt;= int(decimal):

                        #Counts how many zeros need to be added to the end of the binary translation.
                        if 2 ** exp == int(decimal):
                                divTwo = int(decimal)

                                #Loops until all remaining zeros have been accounted for.
                                while divTwo &gt; 1:
                                        divTwo = divTwo / 2
                                        zeros += 1

                        decimal = int(decimal)
                        decimal -= 2 ** exp
                        total += str('1')

                else:
                        total += str('0')

#Places remaining zeros.
while zeros &gt; 0:
        total += str('0')
        zeros -= 1


#Displays answer.
print ('Binary: ' + total)
input()

#
#
#Or 'bin(decimal)' works too, but this was for fun
#
#


Here is a working version of the script.
You can remove half of you code.
You need the variables exp, total and decimal.
The rest are just noise.
You should remove all the int(decimal)s, decimal is only ever subtracted from, and so it's not going to become a float.
And so these are not needed.





You can remove all the code in if 2 ** exp == int(decimal):, instead just use exp as you do zeros.
You should split your while loop so that it only does one thing at a time.
You want an int log and then a while loop that converts the input to binary.
You can replace while zeros &gt; 0: with Pythons way to duplicate strings. 0 * zeros.
You should make this a function.


This leads to code like:

def binary(number): # 4
    output = ''
    if number &lt; 0:
        output = '-'
        number = -number

    exp = 1
    while 2 ** exp &lt;= number: # 2
        exp += 1

    while number &gt; 0:
        exp -= 1
        if 2 ** exp &lt;= number:
            number -= 2 ** exp
            output += '1'
        else:
            output += '0'

    return output + '0' * exp # 1, 3


number = int(input("Enter a whole number: "))
print('Binary: ' + binary(number))