hugh jass hugh jass - 3 months ago 21
Python Question

What is wrong with my python denary to binary program?

i am currently creating a denary to binary program using python and i have no idea why it is not working, if someone could help me and fix my code it would be greatly appreciated.

answer = []
number = 0

denaryNumber = int(input("Type the number you would like to be put in binary (LIMIT IS 255)"))
binary = [128,64,32,16,8,4,2,1]

for x in range (0,8):
if denaryNumber >= binary[number]:
subtraction = binary[number] - (denaryNumber)
answer.append (1)
number += 1
answer.append (0)
number += 1
print (answer)


This is not working because of the following line:

subtraction = binary[number] - (denaryNumber)

You are subtracting from an index of binary that will never be used again, and then assigning it to a variable that is never used. What you intend to do is this:

denaryNumber -= binary[number]