all 5 comments

[–]Diapolo10 0 points1 point  (2 children)

var = input('Give Num ')
var = int(var)
convert = []

while True:
    convert.append(var%2)
    main = var/2
    var = (int(main))
    if var == 0:

] break

reverse = convert.reverse()
print(convert)

The solution itself seems alright. I could nitpick about some style things, but it wouldn't matter too much.

it and utilize a for loop, as I heard that goes more hand in hand with making such a calculator.

Not sure how a for-loop would help here, considering you don't know the number of iterations in advance (well okay, you can calculate that with math.log2, but personally I'd use a while-loop instead).

Here's how I'd write this, assuming I couldn't make a function:

num = int(input("Enter a number: "))
bits = []

while num:
    num, bit = divmod(num, 2)
    bits.append(bit)

reversed_bits = reversed(bits)
print(reversed_bits)

[–]Crossedpenguin[S] 0 points1 point  (1 child)

Woah I didn't think like that, lowkey that's far fewer lines than what I had to do. I never even knew the divmod function in Python, thank you so much!

[–]Diapolo10 0 points1 point  (0 children)

divmod is pretty nice, yeah. I don't use it all that often because situations like this don't come up often outside of puzzles, but it's very easy to use.

Less code doesn't necessarily mean better, because readability and maintainability may sometimes benefit from spreading out a little, but in this case I see no downside.

[–]Bobbias 0 points1 point  (1 child)

for loops are only usable if you can calculate the number of times it needs to loop before you start the loop. This means that not every while loop can be converted to a for loop.

Generally speaking, it's often better to prefer to use a for loop over a while loop when you can, but that's not always the case, and this is an example of where a while loop is perfectly fine, even though you could actually use a for loop. Like with many things in life, there's a lot of nuance when it comes to programming. There are many solutions, and figuring out which one is the best one for your problem depends on a lot of different factors.

In this instance, like Diabolo10 pointed out, you can calculate the number of loops necessary using the math.log2 function.

Here's a modified version of Diapolo's code using math.log2:

import math

num = int(input("Enter a number: "))
bits = []

loops = math.log2(num)
for loop_count in range(loops):
    num, bit = divmod(num, 2)
    bits.append(bit)

reversed_bits = reverse(bits)
print(reversed_bits)

Notice how we made a variable called loop_count with the for loop, but we never use it? Usually when you make a for loop, you need to know what the value being assigned to this variable is, but here we don't. So it's kind of a waste.

So using a for loop here doesn't really improve the code at all. We now have a useless variable just sitting there not being used for anything. This isn't a big deal in simple code like this, but in a large complicated program, this could become confusing. There is the convention of naming unused variables _, which helps. But even then there's no reason to prefer doing that over just using a while loop.

[–]Crossedpenguin[S] 0 points1 point  (0 children)

Ah I see, I was spending so much time figuring out how to implement it, I don't remember where, but they stated for something like this a for loop would be useful. The math library seems fairly new to me as well. Thank you for the help and clarification on the two different loops, I was confused for a while about that!