0

Why is code not working as expected?

When I use the following code, I can't seem to ^2 the output of my function. instead its either adding 2 and subtracting 2. Shouldn't print(oper(a, b)^2) give me an output of 324? def multiply(x, y): return x * y a = 9 b = 2 oper = multiply print(oper(a, b)^2) print((multiply(b,a))^2) print((oper(b,a))-2) print((oper(b,a))+2)

21st Jun 2018, 3:08 PM
Aleksandr Krasner
Aleksandr Krasner - avatar
3 Answers
+ 4
In python ^ is logical XOR operator. And ** is the algebraic exponent operator or commonly said as "to the power". By the way you can also use math.pow(oper(a, b), 2); just include math in the beginning to use the math library.
21st Jun 2018, 4:02 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 12
Replace ^ with ** For exponents in Python there's no ^ symbol instead we use **
21st Jun 2018, 3:16 PM
Nikhil
Nikhil - avatar
+ 1
Many thanks!
21st Jun 2018, 3:22 PM
Aleksandr Krasner
Aleksandr Krasner - avatar