Skip to main content
added 294 characters in body
Source Link

I'm watching this course and at 28:25 it includes this code:

x = 4.5
ans = 0
if x>= 0:
    while ans*ans < x:
        ans = ans +1
    if ans*ans != x:
        print (x, 'is not a perfect square')
    else: print (ans)
else: print (x, ' is a negative number')

Code Image

I don't get the while loop-- does it keep iterating that over and over and THEN checks the if statement? Or does it check the if statement each time it iterates? I feel rather stupid!

Would appreciate some help-- I imagine if I applied myself more I'd get it-- but this stuff is very different from music and philosophy.

(Ah-- got it now-- I didn't think through the indentation. The if and else under while will only take place after the while loop breaks because they're not "inside" the while loop. I swear I get dumber every day. Or maybe it's been a long time since I've done any truly out of domain thinking.)

I'm watching this course and at 28:25 it includes this code:

x = 4.5
ans = 0
if x>= 0:
    while ans*ans < x:
        ans = ans +1
    if ans*ans != x:
        print (x, 'is not a perfect square')
    else: print (ans)
else: print (x, ' is a negative number')

Code Image

I don't get the while loop-- does it keep iterating that over and over and THEN checks the if statement? Or does it check the if statement each time it iterates? I feel rather stupid!

Would appreciate some help-- I imagine if I applied myself more I'd get it-- but this stuff is very different from music and philosophy.

I'm watching this course and at 28:25 it includes this code:

x = 4.5
ans = 0
if x>= 0:
    while ans*ans < x:
        ans = ans +1
    if ans*ans != x:
        print (x, 'is not a perfect square')
    else: print (ans)
else: print (x, ' is a negative number')

Code Image

I don't get the while loop-- does it keep iterating that over and over and THEN checks the if statement? Or does it check the if statement each time it iterates? I feel rather stupid!

Would appreciate some help-- I imagine if I applied myself more I'd get it-- but this stuff is very different from music and philosophy.

(Ah-- got it now-- I didn't think through the indentation. The if and else under while will only take place after the while loop breaks because they're not "inside" the while loop. I swear I get dumber every day. Or maybe it's been a long time since I've done any truly out of domain thinking.)

Source Link

How do if and else statements behave in a while loop (Python)?

I'm watching this course and at 28:25 it includes this code:

x = 4.5
ans = 0
if x>= 0:
    while ans*ans < x:
        ans = ans +1
    if ans*ans != x:
        print (x, 'is not a perfect square')
    else: print (ans)
else: print (x, ' is a negative number')

Code Image

I don't get the while loop-- does it keep iterating that over and over and THEN checks the if statement? Or does it check the if statement each time it iterates? I feel rather stupid!

Would appreciate some help-- I imagine if I applied myself more I'd get it-- but this stuff is very different from music and philosophy.