Making a "break" without any condition, makes the loop useless, so the code only executes once no matter what.
-
If you don't need to keep the original number, you can change "number" as you go.
If you do need to keep the original number, you can make a different variable like "times".
You seem to have mixed these two scenarios together.
-
If you want to print all the steps, the print will be inside the loop so it prints multiple times.
If you only want to print the final result, then the print goes outside the loop.
while number != 0:
remainder = number % 2 # gives the exact remainder
number = number // 2
result = str(remainder) + result
print("The binary representation is", result)
-
The concatenation line:
Putting the print inside the loop might help you see how it works.
we can make an example:
the value in result might be "11010" (a string, with quotes)
the value in remainder might be 0 (an integer, no quotes)
str(remainder) turns the remainder into a string = "0" instead of 0
So when we look at the assignment statement:
result = str(remainder) + result
The right side of the assignment operator = is evaulated first.
The right side of the = is
str(remainder) + result
which, as we went over above has the values:
"0" + "11010"
This is string concatenation. It just puts one string on the end of the other one. The result is:
"0 11010"
"011010"
That is the value evaluated on the right side of the assignment statement.
result = "011010"
Now that is the value of result.
Answer from beauxq on Stack OverflowMaking a "break" without any condition, makes the loop useless, so the code only executes once no matter what.
-
If you don't need to keep the original number, you can change "number" as you go.
If you do need to keep the original number, you can make a different variable like "times".
You seem to have mixed these two scenarios together.
-
If you want to print all the steps, the print will be inside the loop so it prints multiple times.
If you only want to print the final result, then the print goes outside the loop.
while number != 0:
remainder = number % 2 # gives the exact remainder
number = number // 2
result = str(remainder) + result
print("The binary representation is", result)
-
The concatenation line:
Putting the print inside the loop might help you see how it works.
we can make an example:
the value in result might be "11010" (a string, with quotes)
the value in remainder might be 0 (an integer, no quotes)
str(remainder) turns the remainder into a string = "0" instead of 0
So when we look at the assignment statement:
result = str(remainder) + result
The right side of the assignment operator = is evaulated first.
The right side of the = is
str(remainder) + result
which, as we went over above has the values:
"0" + "11010"
This is string concatenation. It just puts one string on the end of the other one. The result is:
"0 11010"
"011010"
That is the value evaluated on the right side of the assignment statement.
result = "011010"
Now that is the value of result.
B_Number = 0
cnt = 0
while (N != 0):
rem = N % 2
c = pow(10, cnt)
B_Number += rem * c
N //= 2
# Count used to store exponent value
cnt += 1
return B_Number
Here is a fix of your code:
number = 123
remainder = 0
x = ""
n = number # don't work on the original number
# you need it for the final print
while n > 0: # wrong syntax for the condition
remainder = (n%2)
x = str(remainder) + x
n = n//2
#(x[::-1]) # this is useless
print(x, "is the binary of", number) # wrong order to print
NB. You should also take advantage of divmod, instead of the two separate % and // operations
Here is what you can do:
number = 5
n = number
x = ""
while n:
x = str(n % 2) + x
n //= 2
print("Decimal:", number)
print("Binary:", x)
Output:
Decimal: 5
Binary: 101
A 1 in the n'th place should represent 2n, not 1n.
answer = answer + (placeHolder ** currentPower)
^^^^^^^^^^^
this is 1 in your code
Hopefully you can see what to fix.
if you suffix 0b with binary in shell, it will give you decimal
>>> 0b111
7
Another way other than int:
>>> def my_bin_dec(x):
... return eval("{:s}".format('0b' +x))
...
>>> my_bin_dec('111')
7
Note:
evalis unsafe to use, but you can useast.literal_eval
def binary(n):
binlist = [int(x) for x in bin(n)[2:]]
return binlist
If you really want to use a while loop:
def binary(n):
binary = list()
while n > 0:
binary.insert(0, n%2)
n /= 2
return binary
How do you expect to get a list if you are never creating one?
You should first, initialize your return value to an empty list [].
And then use append (to add each new item at the end), or insert (to add it to the beginning).