Error shown in Python - TypeError : unsupported operand type(s) for ** or pow() :'str' and 'int' -
i started python today.. spent hours on codeacademy , learnt quite bit thought i'd make own program square ages of person , sibling. basic starter. pretty useless wanted see can do. keep getting typeerror mentioned in title. here's code :
a=raw_input(''enter age'') b=raw_input(''sibling's age'') square=a**2 + b**2 + 2ab if square <160: print 'really young people' else: print 'square of sum >160' please thanks!!
first , foremost, if you're starting, really should learning python 3, not 2.
the code posted has many issues. here's line-by-line breakdown, corrections in python 3:
a=raw_input(''enter age'') b=raw_input(''sibling's age'') you need use either single quotes ' or double quotes ", not 2 single quotes define strings. try:
a = int(input("enter age")) b = int(input("sibling's age")) there no raw_input() in python 3, it's input(). function returns string, if want math on need call int() turn result integer.
square=a**2 + b**2 + 2ab as long a , b ints, first 2 parts of expression fine, need remember operations in python explicit, you'll need fix last part so:
square = a**2 + b**2 + 2*a*b now last part:
if square <160: print 'really young people' else: print 'square of sum >160' python case-sensitive. since used square above, you'll need use same capitalization down here. likewise, builtin keywords , functions in lowercase (if, else, print(), etc.). finally, in python 3 print function, not statement, you'll need parentheses ( ) around arguments:
if square < 160: print("really young people") else: print("square of sum > 160") so, here's complete python 3 code:
a = int(input("enter age")) b = int(input("sibling's age")) square = a**2 + b**2 + 2*a*b if square < 160: print("really young people") else: print("square of sum > 160")