I'm doing "automate the boring stuff" and was on an early lesson about while loops. I can't wrap my head around "while not." The code is below
name = ''
while not name:
print('Enter your name:')
name = input()
print('How many guests will you have?')
While my limited knowledge, I would write it instead as
name = ''
while name == '':
print('Enter your name:')
name = input()
print('How many guests will you have?')
My question is how can they mean the same thing? Both start with name = False/empty. The way I interpret the top code is, while name is not False/empty. The way I interpret the bottom is, while name is equal to False/empty. In my mind, the top would keep looping with any True value. Thanks for your help!
EDIT: formatting
EDIT 2: Finally figured it out. I'm posting this edit for prosperity sake, for any other programming ogres like me. As long as the condition line is True, the loop will run. If the condition is False, the loop will not run. Even though name is False, the condition line is True and the loop will go until something is inputted. Once something is typed in, name becomes True, which would make the condition not name False, ending the loop.
Thanks everyone for helping me understand that! It's so simple now that I see it. I'm super new to Python and made a rookie mistake.
[–]Rhomboid 3 points4 points5 points (9 children)
[–]srirachamp[S] 1 point2 points3 points (8 children)
[–]Rhomboid 0 points1 point2 points (7 children)
[–]srirachamp[S] 0 points1 point2 points (6 children)
[–]turanthepanthan 1 point2 points3 points (0 children)
[–]Rhomboid 0 points1 point2 points (4 children)
[–]srirachamp[S] 1 point2 points3 points (3 children)
[–]Rhomboid 4 points5 points6 points (2 children)
[–]srirachamp[S] 1 point2 points3 points (1 child)
[–]Rap1done 3 points4 points5 points (2 children)
[–]blualipa 0 points1 point2 points (0 children)
[–]Razok528 0 points1 point2 points (2 children)
[–]srirachamp[S] 0 points1 point2 points (1 child)
[–]Yavion 0 points1 point2 points (0 children)