def cube(number): return number^3 print cube(2)
I would expect cube(2) = 8, but instead I’m getting cube(2) = 1
What am I doing wrong?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
^ is the xor operator.
** is exponentiation.
2**3 = 8
Method 2
You can also use the math library. For example:
import math x = math.pow(2,3) # x = 2 to the power of 3
Method 3
if you want to repeat it multiple times – you should consider using numpy:
import numpy as np
def cube(number):
# can be also called with a list
return np.power(number, 3)
print(cube(2))
print(cube([2, 8]))
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0