#๐Ÿ”’ Trying to make a function that convert decimal to binary

42 messages ยท Page 1 of 1 (latest)

faint badge
#
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)
heavy obsidianBOT
#

@faint badge

Python help channel opened

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.

faint badge
#

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?

rotund vortex
#

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

heavy obsidianBOT
#

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)`.
rotund vortex
#

The divmod function pairs well with unpacking.

faint badge
#

oh.. i know floor division and modulo not divmod.. i was lacking logic this was my only logic i came up with

faint badge
rotund vortex
#

There is subtraction involved, so I assume that is the technique I'm thinking of.

faint badge
#

yes num - sq

faint badge
rotund vortex
#

Squaring is not involved in the way I was thinking.

faint badge
#

ye i dont know that method

rotund vortex
#

!e py print(bin(123)) #or print(format(123, 'b'))For the easy way out.

heavy obsidianBOT
faint badge
faint badge
rotund vortex
#

I leave to dinner.

faint badge
#

oh ok nvm thanks for time

muted fog
#

2**i are not squares ๐Ÿฅด

faint badge
#

sorry ๐Ÿ˜ญ

#

poor math

#

power?

muted fog
#

powers of 2, yes

faint badge
#

okk

muted fog
#

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

faint badge
#

oh i have no idea of binary system have to study in uni

muted fog
#

and you can do an odd/even check again

muted fog
faint badge
#

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)

muted fog
#

you need to read up on the basics of the thing you are trying to do pithink

muted fog
#

!e here is the core of it

n = 11
while True:
  print(n%2)
  n //= 2
  if n == 0:
    break
heavy obsidianBOT
heavy obsidianBOT
#
Python help channel closed

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.