1

I'm trying to create a Python program that converts a decimal to binary.

Currently I have

working = int(input("Please select a non-negative decimal number to convert to binary.  "))
x = ()

while working !=0:
    remainder = working % 2
    working = working // 2

    if remainder == 0:
      x = remainder + 0
      print (working, x)

    else:
     x = remainder + 1
    print (working, x)

print ("I believe your binary number is " ,x)

The while works on it's own if I print after that, but the if/else doesn't. I am trying to create a string that is added to with each successive division. Currently, if my starting int is 76, my output is

38 0
38 0
19 0
19 0
9 2
4 2
2 0
2 0
1 0
1 0
0 2

I am trying to get my output to instead be

38 0
19 00
9 100
4 1100
2 01100
1 001100
0 1001100

This is my first attempt at string concatenation and I've tried a few variations of the above code to similar results.

2
  • 2
    Just for clarification: are you aware about bin() ? E.g bin(10) -> 0b1010 Commented Feb 25, 2019 at 5:04
  • Yes, I am, but thank you for checking! I'm trying to show the equation/steps. Commented Feb 25, 2019 at 5:08

4 Answers 4

1

There are a few issues with the code you’ve provided:

  1. x starts with a value of (), and in any case, rather than concatenating strings to it, you’re adding numbers within the loop.
  2. You’re trying to append the numbers rather than prepend, so the result would be reversed if it worked.
  3. Your second print is not inside the conditional, so the output is duplicated.

What you need to do is initialize x with an empty string and then prepend strings to it:

working = int(input("Please enter a non-negative decimal number to convert to binary: "))
x = ""

while working != 0:
    remainder = working % 2
    working = working // 2

    if remainder == 0:
        x = "0" + x
    else:
        x = "1" + x

    print (working, x)

print ("I believe your binary number is", x)

Output:

λ python convert-to-binary.py
Please enter a non-negative decimal number to convert to binary: 76
38 0
19 00
9 100
4 1100
2 01100
1 001100
0 1001100
I believe your binary number is 1001100
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you are not working with strings. You are first creating an empty tuple for x, and then overwriting that with an integer value later.

To do what you are attempting, you need to treat x as a string, and append the string literals '0' and '1' to it.

Try this instead:

working = int(input("Please select a non-negative decimal number to convert to binary.  "))
x = ''

while working !=0:
    remainder = working % 2
    working = working // 2

    if remainder == 0:
        x += '0'
        print (working, x)

    else:
        x += '1'
        print (working, x)

print ("I believe your binary number is " , x[::-1])

Note how x is initially declared as an empty string '' instead of the empty tuple (). This makes it so that when you use the += operator later to append 0 or 1 to it, that it is treated as string concatenation instead of addition.

Comments

0

It should be

working = int(input("Please select a non-negative decimal number to convert to binary.  "))
x = ""

while working !=0:
    remainder = working % 2
    working = working // 2

    if remainder == 0:
      x = x + str(remainder)
      print (working, x)

    else:
     x = x + str(remainder)
    print (working, x)

print ("I believe your binary number is " ,x[::-1])

Comments

0

Change your code to below:

if remainder == 0:
    x = str(remainder) + '0'
    print (working, x)

else:
    x = str(remainder) + '1'
    print (working, x)

in your code, python interprets as an int you have to cast it to string.

another way is using built-in function bin(working), directly converts from number to binary value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.