0

Here is a small snipet from the code which is causing the error.

num = input('Binary number? ')
base = input('base? ')

baseTen = 0
n = 1
for j in num:
    product = j*base**(len(num)-n)
    n += 1
    baseTen = baseTen + product
    print(baseTe)

The error message I recive is this:

    product = j*base**(len(num)-n)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

I am not sure what exactly is happening. Ive tried surrounding it with int() but that did not work. Thanks for any help in advance

  • 2
    `input()` returns a string. – Martijn Pieters Nov 29 '15 at 23:29
  • Also see [Convert binary string to int](http://stackoverflow.com/questions/8928240/convert-binary-string-to-int) – Felk Nov 29 '15 at 23:33
  • I'm surprised you aren't getting an error on `for j in num:` – Gi0rgi0s Nov 29 '15 at 23:36
  • Try this: `num = input('Binary number? ') base = input('base? ') baseTen = 0 n = 1 for j in str(num): product = int(j)*base**(len(str(num))-n) n += 1 baseTen = baseTen + product print(baseTen)` Also, I think you are a little confused b/c you are asking the user to enter a base. Since it is a conversion to binary, the input should always be 2. – Gi0rgi0s Nov 29 '15 at 23:54

0 Answers0