[Tutor] Cube root
Steven D'Aprano
steve at pearwood.info
Sun Sep 16 03:33:23 CEST 2012
On 16/09/12 07:28, Amanda Colley wrote:
> Ok, I have to get input from a user ('enter a number') and then get the
> cube root of that number. I am having trouble with the code to get the
> cube root. If anyone can help me solve this I would greatly appreciate it.
> ('enter a number')
> n=number
> ??? cube root??
The definition of "cube root of x" is "x to the power of one third".
To calculate cube root, you need to get the number from the user and raise
it to the power of one third. This is actually a subtle problem:
* input you get from the user is a string, you need to turn it into a
number
* you have a choice between using the ** operator or the pow() function
for raising x to a power
* the obvious solution:
x**1/3
is wrong. Can you see why? (Hint: there are two operators there, power
and divide. What order do they get executed?)
* even more subtle: there is a difference in how Python does division
depending on the version you use. Calculating one third correctly is
slightly trickier than it seems.
I hope this is enough hints for you to solve the problem. If not, show
us the code you have and ask for more help.
--
Steven
More information about the Tutor
mailing list