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'
CodePudding user response:
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"))
CodePudding user response:
["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:
- the digit value (2 or 9 in my example, but just 0 or 1 in a binary number)
- the base (10 in my example, but 2 in your example)
- 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.
CodePudding user response:
To be honest, I don't know what will be the output. But you are trying to calculate between str and int. On the second line binairy ** 2
- Here binairy is Str. You can convert it to int by int(binairy)
. Then do the calculation you want.
If I work on your code:
for binairy in ["101011101"]:
binairy = int(binairy) ** 2
print(binairy)
print(len(str(binary)))
Output:
10203242525232201
17
CodePudding user response:
I think I understand what you mean: You are willing to make the weight of every single number and than summing it all like "110" = "0x20 1x21 1x22" = 62dec
So I will suggest to write the code like this:
weight, result = 0, 0
for binary in "101011101"[::-1]:
result = int(binary)*2**weight
print(result)