tags:

views:

691

answers:

5

I have this python code:

def sqrt(x):
    ans = 0
    if x >= 0:
     while ans*ans < x:
      ans = ans + 1
      if ans*ans != x:
       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   
sqrt(y)

the output is:

16 is not a perfect square.

Whereas this works perfectly:

x = 16
ans = 0
if x >= 0:
    while ans*ans < x:
     ans = ans + 1
     #print 'ans =', ans
    if ans*ans != x:
     print x, 'is not a perfect square' 
    else: print ans, 'is a perfect square'
else: print x, 'is not a positive number'

What am I doing wrong?

A: 

EDIT I modified it, tried it out, and it works. You just need this piece of code

As soon as ans = 4, ans * ans is no longer smaller than x. Try while ans*ans <= x: instead of just <

def sqrt(x):
ans = 0
if x >= 0:
     while ans*ans <= x:               
             if ans*ans == x:
                      print x, ' is a perfect square.'
                      return ans
  else:
   ans = ans + 1
ManicMailman
Tried, still the same.
Nimbuz
You can't return in your loop in the case where ans*ans is not x.def sqrt(x): ans = 0 if x >= 0: while ans*ans <= x: ans = ans + 1 if ans*ans == x: print x, ' is a perfect square.' return ans else: print x, ' is not a positive number.' return None print x, 'is not a perfect square.' return None y = 16 sqrt(y)
ManicMailman
+ a print "not square" after the while to make it right..
stiank81
I know, I was just taking a minimalist approach to make things more obvious
ManicMailman
+1  A: 

Your while loop only executes once. No matter which branch the if statement inside it takes, the whole function will return immediately.

Greg Hewgill
Strange! The same code works outside the function, why is that?
Nimbuz
Because the loop outside the function is not being exited prematurely with a return.
paxdiablo
A: 

Change your code so it displays the value of ans as well as x, so you can tell how many times the loop is executed.

pavium
ermm..can you please post the modified code? Thanks
Nimbuz
I'm not a python programmer, but I could recognise the same problem as Greg Hewgill.
pavium
+5  A: 

Indent your code correctly to let the while statement execute until ans*ans < x:

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)

Try it out here.

CMS
Aah! I was going crazy over this, indentation while really helps in reading the code can be really difficult to debug! :)
Nimbuz
Give me Python's indentation rules any day over the possibility my indentation may not match my braces in C et al. It's a lot easier if you make sure your editor is configured for spaces only.
paxdiablo
A: 

If your code sample is actually correctly indentet the first round of the while will return on it's first round - always. So any positive value of x>1 will fullfil the ans*ans=1*1=1!=x, giving "x is not a perfect square".

You basically needs to get your indentation right - like you do in your other example. Again - if your code sample here actually is correctly indented. Try this:

def sqrt(x):
    ans = 0
    if x >= 0:
        while ans*ans < x:
            ans = ans + 1

        if ans*ans != x:
            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
stiank81