Search code examples
pythonpseudocode

Binary string to Decimal integer converter


I am trying to write a program that converts a 4-bit string representation of binary into a decimal (base 10) integer.

This is what I got so far, but after I type In the 4-bit binary (e.g. 1101) It just comes out with: '>>>'.

Here is the flow diagram I am following:

Here is my code:

def binaryToDenary():
    Answer = 0
    Column = 8
    Bit = int(input("Enter bit value: "))
    if Column >1:
        Answer = Answer + (Column * Bit)
        Column = Column/2
    elif Column <1:
        print("The decimal value is: " + Answer)


binaryToDenary()

What am I doing wrong? Any hints?


Solution

  • It looks like you haven't implemented the loop:

    def binaryToDenary():
        Answer = 0
        Column = 8
        while not Column < 1:
            Bit = int(input("Enter bit value: "))
            Answer = Answer + (Column * Bit)
            Column = Column/2
        print("The decimal value is: {}".format(Answer))