cpt_azad
01-30-2007, 02:43 AM
The question asks for a program that converts decimal to binary using the division method (divide by 2, get rid of remainder, divide by 2, etc etc).
I'm having trouble comprehending how to go about doing this in Python, like how do I save the fact that if theres a remainder then its a 1 (in binary/base2) and if there is no remainder that it's a 0 (in binary/base2).
This is what I have so far:
num = int(raw_input("Please input a number in the range [0, 127]: "))
if not(num > 0 and num <= 127):
print "You entered an invalid number, please restart the program."
num1 = num/2
num2 = num1/2
num3 = num2/2
num4 = num3/2
num5 = num4/2
num6 = num5/2
num7 = num6/2
num8 = num7/2
I'm having trouble comprehending how to go about doing this in Python, like how do I save the fact that if theres a remainder then its a 1 (in binary/base2) and if there is no remainder that it's a 0 (in binary/base2).
This is what I have so far:
num = int(raw_input("Please input a number in the range [0, 127]: "))
if not(num > 0 and num <= 127):
print "You entered an invalid number, please restart the program."
num1 = num/2
num2 = num1/2
num3 = num2/2
num4 = num3/2
num5 = num4/2
num6 = num5/2
num7 = num6/2
num8 = num7/2