0

i am a beginner with python. I need to calculate a binairy number. I used a for loop for this because I need to print those same numbers with the len(). Can someone tell me what i am doing wrong?

for binairy in ["101011101"]:
    binairy = binairy ** 2
    print(binairy)
    print(len(binairy))

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

6
  • binairy is a string variable and power operation doesn't support string variable as an argument Commented Sep 21, 2022 at 18:59
  • You can type int("101011101", 2) if you want your binary value to be converted to base 2. Commented Sep 21, 2022 at 19:00
  • @std124_lf you mean int("101011101", 2) converts the string from base 2 (and to an integer), right? Commented Sep 21, 2022 at 19:02
  • @thebjorn it converts a string to the specified base (in this case base 2) Commented Sep 21, 2022 at 19:03
  • @std124_lf nope, it converts from that base, i.e. int("11", 16) == 17 converting from base 16, and int("11", 2) == 3 converting from base 2. Commented Sep 21, 2022 at 19:05

2 Answers 2

1

you used a list but you could use a string directly

def binary_to_decimal(binary):
    decimal = 0
    for digit in binary:
        decimal = decimal*2 + int(digit)
    return decimal


print(binary_to_decimal("1010"))
Sign up to request clarification or add additional context in comments.

Comments

0

["101011101"] is a list with a single string. "101011101" is a list of characters. Look at this:

for let_me_find_out in ["101011101"]:
    print(let_me_find_out)

for let_me_find_out in "101011101":
    print(let_me_find_out)

With this knowledge, you can now start your binary conversion:

for binairy in "101011101":
    binairy = binairy ** 2
    print(binairy)

That code gives you the error:

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

It means that you have the operator ** and you put it between a string and a number. However, you can only put it between two numbers.

So you need to convert the string into a number using int():

for binairy in "101011101":
    binairy = int(binairy) ** 2
    print(binairy)

You'll now find that it still gives you 1s and 0s only. That is because you calculated 1^2 instead of 2^1. Swap the order of the operands:

for binairy in "101011101":
    binairy = 2**int(binairy)
    print(binairy)

Now you get 2s and 1s, which is still incorrect. To deal with this, remember basic school where you learned about how numbers work. You don't use the number and take it to the power of something. Let me explain:

The number 29 is not calculated by 10^2 + 1^9 (which is 101). It is calculated by 2*10^1 + 9*10^0. So your formula needs to be rewritten from scratch.

As you see, you have 3 components in a single part of the sum:

  1. the digit value (2 or 9 in my example, but just 0 or 1 in a binary number)
  2. the base (10 in my example, but 2 in your example)
  3. the power (starting at 0 and counting upwards)

This brings you to

power = 0
for binairy in "101011101":
    digit = int(binairy)
    base = 2
    value = digit * base ** power
    print(value)
    power += 1

With above code you get the individual values, but not the total value. So you need to introduce a sum:

power = 0
sum = 0
for binairy in "101011101":
    digit = int(binairy)
    base = 2
    value = digit * base ** power
    sum += value
    print(value)
    power += 1
print(sum)

And you'll find that it gives you 373, which is not the correct answer (349 is correct). This is because you started the calculation from the beginning, but you should start at the end. A simple way to fix this is to reverse the text:

power = 0
sum = 0
for binairy in reversed("101011101"):
    digit = int(binairy)
    base = 2
    value = digit * base ** power
    sum += value
    print(value)
    power += 1
print(sum)

Bam, you get it. That was easy! Just 1 hour of coding :-)

Later in your career, feel free to write print(int("101011101", 2)) instead. Your colleagues will love you for using built-in functionality instead of writing complicated code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.