def makebin(num):
every_squares = []
for i in range(0, num):
power = 2**i
if power > num:
break
every_squares.append(power)
reminder = None
nums = []
while reminder != 0:
print(reminder)
sq = 0
for i in range(0, num):
x = 2**i
if x > num:
print("breaked")
break
sq = 2**i
print(sq, "sq", num)
reminder = num - sq
num = reminder
nums.append(sq)
print(nums, every_squares)
binary = ""
for n in every_squares[::-1]:
if n in nums:
binary += "1"
else:
binary += "0"
return binary
x = makebin(75)
print(x)
#๐ Trying to make a function that convert decimal to binary
42 messages ยท Page 1 of 1 (latest)
@faint badge
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
i did something like this to get the square number most close to the num py while reminder != 0: print(reminder) sq = 0 for i in range(0, num): x = 2**i if x > num: print("breaked") break sq = 2**i print(sq, "sq", num) reminder = num - sq this make sense like for a number 75 i wanted to get the square 64 is my method ok?
What you want to look at is the floor division operator, the modulo operator and the divmod function.
Also the format and bin functions.
!d divmod
divmod(a, b)```
Take two (non\-complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as `(a // b, a % b)`. For floating\-point numbers the result is `(q, a % b)`, where *q* is usually `math.floor(a / b)` but may be 1 less than that. In any case `q * b + a % b` is very close to *a*, if `a % b` is non\-zero it has the same sign as *b*, and `0 <= abs(a % b) < abs(b)`.
The divmod function pairs well with unpacking.
oh.. i know floor division and modulo not divmod.. i was lacking logic this was my only logic i came up with
i actually saw a video on how to convert decimal to binary with subtraction method i thought of implementing it... does the subtraction method need this floor division or modulo isnt the division method the one that requires this.? i dont know much about binary numbers too
There is subtraction involved, so I assume that is the technique I'm thinking of.
yes num - sq
my actual question was is this code messy i neeed to find the square number most close to the num (75 here on 1st iteration) so i can do num -sq
Squaring is not involved in the way I was thinking.
ye i dont know that method
!e py print(bin(123)) #or print(format(123, 'b'))For the easy way out.
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0b1111011
002 | 1111011
the value of sq will be of its last iteration like 64 in first iteration of while
ye i did that first but was curios to implement this
I leave to dinner.
oh ok nvm thanks for time
2**i are not squares ๐ฅด
powers of 2, yes
okk
but yeah, there are way easier ways of doing this process
if you have say 11 (1011 in base 2), you can extract the rightmost bit easily
by just checking if 11 is odd/even
oh i have no idea of binary system have to study in uni
then you can do something to get rid of the rightmost bit, leaving you with 101
and you can do an odd/even check again
it's not really much different to how our decimal system works
hmm
yeh ill learn it someday... actually this was programming question on exercism.org i spent almost 1.5hours on it lol
i need to improve my logic.. like i spend lot of time behind flawed logic.. maybe its because am still new programming (1year)
you need to read up on the basics of the thing you are trying to do 
!e here is the core of it
n = 11
while True:
print(n%2)
n //= 2
if n == 0:
break
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 1
003 | 0
004 | 1
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.