Home>

I wrote a code to convert decimal numbers to binary numbers in python.

Error message

There is no output when moving. The moved environment is Anaconda.

Error message

None

Applicable source code

`` `python

def dec2bin (target):
amari = []

while target! = 0:
amari.append (target% 2)
target = target // 2

amari.reverse ()
return amari

I entered a value directly in target

Supplemental information (FW/tool version etc.)

Operating environment: windows10, Anaconda (jupyter)

  • Answer # 1

    It has moved.

    >>>def dec2bin (target):
    ... amari = []
    ... while target! = 0:
    ... amari.append (target% 2)
    ... target = target // 2
    ... amari.reverse ()
    ... return amari
    ...
    >>>dec2bin (123)
    [1, 1, 1, 1, 0, 1, 1]
    >>>dec2bin (127)
    [1, 1, 1, 1, 1, 1, 1]
    >>>dec2bin (128)
    [1, 0, 0, 0, 0, 0, 0, 0]

    Bin function or binary conversion function is standard in python.

    >>>bin (123)
    '0b1111011'
    >>>f "{123: b}"
    '1111011'

Related articles