0

I'm getting the syntax error: Traceback (most recent call last): File "python", line 4, in TypeError: unsupported operand type(s) for ** or pow(): 'unicode' and 'int'

Edit: Okay so this is my new code it'll reset once it's done so you can enter another number until you enter something that isn't a number

 while True:
    print 'Welcome to the "Square Root Calculator"'
    print
    number = float(raw_input ('Insert a number: '))
    answer = number ** (1./2)
    print
    if number:
        print ('Square Root: ' + str(answer))
    else:
        break
    print '#=====================================================#'
    print '|                                                     |'
    print '#=====================================================#'
pass
Zerp
  • 3
  • 4

2 Answers2

0

raw_input is returning a string, probably - see this question, for instance. Try doing int(number) - but be prepared that your person might not give you an integer!

Community
  • 1
  • 1
kcrisman
  • 4,374
  • 20
  • 41
0

You need to modify the code in several places- First you need to add int around raw_input ('Insert a number: '), otherwise number is of type str. You also need to add str around answer, because you cannot add a string and a number. Finally, replace 1/2 with 1./2, as on python 2, 1/2 is 0. In order to make empty rows, you could add \n in the appropriate places.

Here is the corrected version:

print 'Welcome to the "Square Root Calculator\n'
number = float(raw_input ('Insert a number:'))
answer = number ** (1./2)
if number:
    print ('\nSquare Root: ' + str(answer))
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
  • So adding 'int' before a raw_input makes it so that the character they type in will be characterized as a number? – Zerp Apr 26 '17 at 02:42
  • Yes, specifically it would be characterized as int. You could replace int with float if you want to allow float inputs as well. – Miriam Farber Apr 26 '17 at 02:44
  • are there any differences between int and float that could potentially cause code to not work? – Zerp Apr 26 '17 at 02:46
  • If you want to find the square root of say 5.4, the code will not allow you to do that and will throw an error. So if you need to find a root of non integer numbers as well, you should change the int into float. – Miriam Farber Apr 26 '17 at 02:47
  • So is it better to just use float all the time so you can use any number? – Zerp Apr 26 '17 at 02:52
  • Yes definitely use float if you want to allow any input. I updated the answer. – Miriam Farber Apr 26 '17 at 02:54
  • Okay I wrote a bit of new code with what you gave me and now it loops and if you enter something that isn't a number it closes. – Zerp Apr 26 '17 at 02:59
  • @Zerp if this has helped solve your problem, consider up-voting or accepting the answer(s) here :) – David Zemens Apr 26 '17 at 03:03