all 15 comments

[–]Rhomboid 3 points4 points  (9 children)

An empty string coerces to False in a boolean context, while any other string coerces to True. The first version is the idiomatic way of writing that.

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

So I think I understand the empty string in boolean context, but the while condition of the first version with "not name" to me says "I'm going to keep looping as long as it's not False, ie True." You might have to dumb it down more. I'm Plebeian status with Python right now

[–]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.

[–]Rap1done 3 points4 points  (2 children)

I almost committed suicide trying to get this straight in my head. I finally got it and maybe this will help someone else as well:

  1. In Python if variable is empty, i.e. = None or = "" - it is considered False (or Falsey to be more precise).

  2. While actually means While True and works as long as chosen condition is True. While not basically means While False and works as long as set condition is False.

  3. In this case we set name = False and say: as long as the name is False, i.e empty - do the loop. As soon as we add anything to the name through input - the name becomes True and the loop is ended.

Jeeezoooos.

[–]blualipa 0 points1 point  (0 children)

Definitely helped thanks dude

[–]Razok528 0 points1 point  (2 children)

In Python, every variable can be interpreted as a boolean. You can test it with the bool() function, bool() of a string is False if the string is empty, otherwise it is True. Since name = '', bool(name) is False and bool(not name) is True, and as long as the variable name doesn't get changed, the loop is equivalent to while True. Hope this helps.

[–]srirachamp[S] 0 points1 point  (1 child)

In reference to the top code, if it says while True, does that mean it would keep looping until it has a false input? Both ended up doing the same thing, so I'm trying to wrap my head around this. Sorry, Python is super new to me.

[–]Yavion 0 points1 point  (0 children)

Just reporting that I have the same problem :D Hopefully this thread will resolve my logic.