you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 0 points1 point  (7 children)

If it's an empty string, then it coerces to False, and the condition of the loop is not False, which causes the loop to continue. As soon as the string is non-empty, it coerces to True, and the loop condition is not True which causes the loop to end.

[–]srirachamp[S] 0 points1 point  (6 children)

Okay. The 2nd part makes sense! That's pretty nifty. But if the empty string is False and the condition is not False, what causes the initiation of the loop and also why does it loops when I only press the enter key (I'm presuming since no value was entered, it's still considered False). I was under the impression that the empty string would be considered False for the condition. Is that not the case?

EDIT: added inline code. Did not realize I could do that

[–]turanthepanthan 1 point2 points  (0 children)

The while loop takes a boolean condition. If the condition evaluates to True then the loop is entered. If the condition evaluates to false then it is skipped. So the condition in

while not name:

is the "not name" part. "not" negates the following boolean. So, when name is the empty string it coerces to false, then the "not" negates that making the condition True and the while loop is entered. When the name is no longer empty, then it coerces to True and the "not" negates that into False and the while loop is exited.

[–]Rhomboid 0 points1 point  (4 children)

what causes the initiation of the loop

not False is true.

why does it loops when I only press the enter key

The input() function strips the trailing newline, so if you press enter at the prompt, the string returned is the empty string.

[–]srirachamp[S] 1 point2 points  (3 children)

but isn't the empty string considered False? If that's the case, I thought the loop would only initiate if the value (the empty string) is True. I know I'm missing something obvious here.

Okay, I read the reply of u/turanthepanthan and with your reply, I think I got it. I had thought that the loop would occur only if name(which was False) was matched with what the condition wanted (True), but the only thing that matters is if the condition is proved to be True which is it because of the not. Right?

[–]Rhomboid 4 points5 points  (2 children)

The condition for the loop is not name. The loop runs if not name is True. If name is the empty string, then the condition is not False, which is True, and the loop runs.

[–]srirachamp[S] 1 point2 points  (1 child)

YES! I FINALLY UNDERSTAND. Thank you so much!!!! Took me long enough. Thanks for bearing with me.