擅长:python、mysql、java
<p>正确缩进代码,让<code>while</code>语句执行到<code>ans*ans < x</code>:</p>
<pre><code>def sqrt(x):
ans = 0
if x >= 0:
while ans*ans < x:
ans = ans + 1
if ans*ans != x: # this if statement was nested inside the while
print x, 'is not a perfect square.'
return None
else:
print x, ' is a perfect square.'
return ans
else:
print x, ' is not a positive number.'
return None
y = 16
print sqrt(y)
</code></pre>
<p>试试看<a href="http://codepad.org/dnxYtH5t" rel="noreferrer">here</a>。</p>