0

I need to create a program where I input a six digit binary number, and for it to output the decimal equivalent. But I'm struggling to figure out how to match the input digits to their equivalent powers of 2 list.

So 100001 should grab the first item in my powers of 2 list - 32, and the last item, 1.

powersof2 = [32, 16, 8, 4, 2, 1]
e = []

i = str(input("Enter: "))
for a in powersof2:
    for x in i:
        if "0" in x:
            continue

        if "1" in x:
            e.append(a)
print(e)

Obviously at the moment, it's just printing all of the powersof2 list, rather than individually because I can't figure out how to make it continue to the next item in powrsof2?

Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
  • This should do the trick, although it's not exactly a general answer to "how to iterate two iterators in parallel: `[1< – rici Jan 10 '23 at 03:47

1 Answers1

-1

Based on your code:

powersof2 = [32, 16, 8, 4, 2, 1]
e = []

i = str(input("Enter: "))
for a, x in zip(powersof2, i):
    if "0" in x:
        continue

    if "1" in x:
        e.append(a)

print(sum(e))
Ric
  • 5,362
  • 1
  • 10
  • 23