Friday, 6 September 2013

Converting 16 bit binary number to decimal using only if-else statements

Converting 16 bit binary number to decimal using only if-else statements

I need to write a program that asks the user to enter a 16-bit binary
number and then converts it to decimal. I am only allowed to use the
if-else statement (no for/while etc.. and no functions).
My problem is not the conversion itself.
My problem is representing the user's number as a 16-bit number once he
entered a number with less then 16 digits. For example, if the user
entered "1111" I need to convert it to "0000 0000 0000 1111".
I tried :
A=int(input("Please enter a 16-bit decimal number:"))
B=0000000000000000
C=A or B
print (C)
which gave me the original number in A (I don't realy understand why).
I also tried to look at the user's input as a string but, as I understand
it, Python starts to number the chars from left to right so in case of
A=1111 I have only A[0]-A[3] so I was kind of stuck with it.
I know there are functions that I can use but, the assignment is to use
only the subjects we learned in class which is basically only the if-else
statement (and assignment statements).
Thank you. Yul

No comments:

Post a Comment