I am fairly new to python and I am trying to make a code that converts any character to its binary equivalent. I so far have some code that displays the right result, but you have to read it form the bottom upwards. For example, if you input the character "F", you get the result 0 1 1 0 0 0 1 0 (which should be read 0 1 0 0 0 1 1 0).
letter = input("Please enter any character : ")
ascii_code = (ord(letter))
x = 0
for x in range(0,8):
binary = (ascii_code)%2
ascii_code = (ascii_code)//2
print(binary)
x =+1
Any ideas on how to fix this so it is displayed properly? Thank you :)
If you're asking how to make print not add a newline, use the option
endand set it to an empty string.Then when the loop ends you can print a newline:
print()To get the order reversed you'd want to create a string bit by bit in the loop, then reverse it (
reversed()orresult[::-1]) and print it all at once when the loop exits.