I don't have any experience with coding at all and I have just started learning Python.
In John Guttag's book "Introduction to Computation and Programming Using Python With Application to Understanding Data", at the beginning of chapter 3, there is a code example:
#Find the cube root of a perfect cube
x = int(input('Enter an integer: '))
ans = 0
while ans**3 < abs(x):
ans = ans + 1
if ans**3 != abs(x):
print(x, 'is not a perfect cube')
else:
if x < 0:
ans = -ans
print('Cube root of ' + str(x) + ' is ' + str(ans))
What I have a hard time understanding is how "if" outside of "while" can pick up from where the loop left off with the iteration? If it is because of the last iteration of ans which gets it out of the loop and also satisfies the if condition, how does the if condition work for the values of ans and thus x inside the loop?(ans^3 is not equal to x only inside the while loop, how come can this part work:
if ans**3 != abs(x):
print(x, 'is not a perfect cube')
I really don't know how else to ask this but this is the code that I came up with before I peeked at the code in the book, it worked, and maybe it wil help to clarify what I am exactly asking:
x=int(input('Enter an integer: '))
crx=0
while True:
if crx**3<abs(x):
crx=crx+1
elif crx**3==x:
print('The cube root of',x,'is',str(crx)+'.')
break
elif crx**3==-x:
print('The cube root of',x,'is',str(-crx)+'.')
break
else:
print(x,'is not a perfect cube.')
break
In my mind, somehow, I had to insert the if code blocks inside the while loop...
Thank you in advance.
This is not a consequence of if
going back to the while
loop. Let's examine the control flow:
x is being set as an integer from string input
ans
is initialized with value 0
, an int
x = int(input('Enter an integer: '))
ans = 0
To check if something has a cube root, the while loop takes every integer greater than 0
and cubes it, if the cube is less than x
, then we increase ans
by 1
, otherwise, ans
is saved and the while
loop exits. Note, the otherwise covers if the cube is greater than or equal to x
.
while ans**3 < abs(x):
ans = ans + 1
If the resulting ans
is equal to x
, then x
has a cube root. If it is not, then x
is not a cube root.
if ans**3 != abs(x):
print(x, 'is not a perfect cube')
else:
if x < 0:
ans = -ans
print('Cube root of ' + str(x) + ' is ' + str(ans))
In simple terms - the loop makes sure the value of ans
is the lowest (non-negative) integer whose cube is greater than or equal to x
. Note the following consequence:
x
is a perfect cube, then the final value of ans
will be exactly that cube root (because we know that ans**3
must be equal to x
.ans
must be such that ans**3
is strictly greater than x
. In particular, it will not be equal to it. So the calculation is purely done in the while
loop - and after this, the if
checks if you calculated an exact cube root, or whether you don't (in which case this was never possible, because x
was not a perfect cube). And of course it prints an appropriate message to the user in either case.
(I just saw the edit with your own solution - yes, as you observed, that also works fine. There is rarely only one way to solve a problem with code.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.