Skip to main content
Fixed a bug, with 2.
Source Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155
  • 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.

  1. You can remove all the code in if 2 ** exp == int(decimal):, instead just use exp as you do zeros.

  2. 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.

  3. You can replace while zeros > 0: with Pythons way to duplicate strings. 0 * zeros.

  4. You should make this a function.

This leads to code like:

def binary(number): # 4
    output = ''
    if number < 0:
        output = '-'
        number = -number

    exp = 1
    while 2 ** exp <<= number: # 2
        exp += 1

    while number > 0:
        exp -= 1
        if 2 ** exp <= 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))
  • 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.

  1. You can remove all the code in if 2 ** exp == int(decimal):, instead just use exp as you do zeros.

  2. 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.

  3. You can replace while zeros > 0: with Pythons way to duplicate strings. 0 * zeros.

  4. You should make this a function.

This leads to code like:

def binary(number): # 4
    output = ''
    if number < 0:
        output = '-'
        number = -number

    exp = 1
    while 2 ** exp < number: # 2
        exp += 1

    while number > 0:
        exp -= 1
        if 2 ** exp <= 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))
  • 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.

  1. You can remove all the code in if 2 ** exp == int(decimal):, instead just use exp as you do zeros.

  2. 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.

  3. You can replace while zeros > 0: with Pythons way to duplicate strings. 0 * zeros.

  4. You should make this a function.

This leads to code like:

def binary(number): # 4
    output = ''
    if number < 0:
        output = '-'
        number = -number

    exp = 1
    while 2 ** exp <= number: # 2
        exp += 1

    while number > 0:
        exp -= 1
        if 2 ** exp <= 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))
Source Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155

  • 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.

  1. You can remove all the code in if 2 ** exp == int(decimal):, instead just use exp as you do zeros.

  2. 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.

  3. You can replace while zeros > 0: with Pythons way to duplicate strings. 0 * zeros.

  4. You should make this a function.

This leads to code like:

def binary(number): # 4
    output = ''
    if number < 0:
        output = '-'
        number = -number

    exp = 1
    while 2 ** exp < number: # 2
        exp += 1

    while number > 0:
        exp -= 1
        if 2 ** exp <= 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))