0

I am new to using python so I apologize if I do not use the correct terms. I am writing a simple program to where the user inputs a decimal number and the program converts it to a binary number. So far I have:

remainder=0
decimal_number= int(input('Enter the decimal number:'))

while decimal_number != 0:
    (decimal_number, remainder) = divmod(decimal_number,2)
    print(remainder)

If I input 11, the output looks like

1
1
0
1

My question is how can I format it to be in one single line, and how can I reverse is so it is written correctly? (ex. from the output shown above to '1011'). I am also aware there is a binary function, but I am not allowed to use it in my program.

Ho0ony
  • 158
  • 11
BarbaraJ
  • 21
  • 4
  • probably use `print(remainder, end='')` – chickity china chinese chicken Sep 25 '17 at 04:47
  • See [this question for some good facts about printing](https://stackoverflow.com/questions/12032214/print-new-output-on-same-line). But basically, the `print` function takes keyword parameters `sep` and `end` that default to `' '` and `'\n'`, respectively. You can pass `print` whatever you want... – juanpa.arrivillaga Sep 25 '17 at 04:48
  • Thank you! This gave me the output of '1101', but how do I reverse it to look like '1011'? – BarbaraJ Sep 25 '17 at 04:50
  • since you are a beginner, I encourage you to try to solve it on your own. Try putting it in a data structure, like a `str` or even a `list` of numbers, then try figuring out how to print from the *end* of the list using a loop and indexing, i.e. `my_list[i]` is the *ith* value of `my_list`. – juanpa.arrivillaga Sep 25 '17 at 05:00
  • @BarbaraJ, if you get the output as a list, you could use the `reverse` function on the list, like in this answer [How can I reverse a list in python?](https://stackoverflow.com/q/3940128/1248974) – chickity china chinese chicken Sep 25 '17 at 05:10

6 Answers6

0

Try using

import sys
remainder=0
decimal_number= int(input('Enter the decimal number:'))
    while decimal_number != 0:
    (decimal_number, remainder) = divmod(decimal_number,2)
    sys.stdout.write(str(remainder))

That should print everything on the same line :)

If you want some explanation,

we import the sys module first which gives us access to the stdout.write() method.

This method doesn't automatically insert a new line so it fits your purpose. We also convert the integer to string since the sys.stdout.write() method will not accept integer values.

Manan Adhvaryu
  • 323
  • 3
  • 14
0

While @Manan's answer will print the values on the same line, it is not very pythonic and does not reverse the result as you require.

I would suggest storing the values you need and then reversing them:

remainder=0

decimal_number= int(input('Enter the decimal number:'))

result = ''
while decimal_number != 0: 
    (decimal_number, remainder) = divmod(decimal_number,2)
    result += str(remainder)
print(result[::-1])

Alternatively you could repeatedly append to the beginning of your stored result to achieve the reversal:

remainder=0

decimal_number= int(input('Enter the decimal number:'))

result = ''
while decimal_number != 0: 
    (decimal_number, remainder) = divmod(decimal_number,2)
    result = str(remainder) + result
print(result)
Kent Sommer
  • 358
  • 3
  • 7
0

To print binary results in a single line use end='' as second argument in the print function to prevent printing a new line after printing the result. Or you can declare a variable and concatenate the string value of binary result and print is out side of the loop, Like

binaryResult = ''
while decimal_number != 0: 
    (decimal_number, remainder) = divmod(decimal_number,2)
    binaryResult += str(remainder)
print(result)

Good luck

alaminio
  • 82
  • 2
  • 12
0

When you run following script, be aware the indent. you will get: 1101

def test():
    remainder=0

    decimal_number= int(input('Enter the decimal number:'))

    while decimal_number != 0: 

        (decimal_number, remainder) = divmod(decimal_number,2)
        print (remainder,end="")         

if __name__ == '__main__':
    test()

If you have much much bigger binary value need to be reverse, binary tree(data structure) can help with. But list is the easiest way.

LunaRivolxoxo
  • 330
  • 2
  • 5
0

Use a simple and clean approach

decimal_number= input('Enter the decimal number:')
binary = bin(decimal_number)
print binary[2:]

Output

Enter the decimal number:10
1010
Amarpreet Singh
  • 2,242
  • 16
  • 27
0

print is a function that accepts keyword end to specify how the end of each output should be handled. The default end argument is the newline character '\n': end=’\n’, which we can change to be '', essentially nothing.

The other issue to print from most significant bit (LSB) to least significant bit (MSB) could be handled building a list and print it with the reversed function.

Putting it all together:

remainder=0

decimal_number= int(input('Enter the decimal number:'))

result = []

while decimal_number != 0: 
    (decimal_number, remainder) = divmod(decimal_number,2)
    result.append(remainder)

for n in reversed(result):
    print(n, end='')
print()

demo:

Enter the decimal number:11
1011

Hope this helps.