简体   繁体   中英

I can't quite understand how this while loop works

This code essentially prompts the user to enter their name and say hello to them and if no value is entered by the user is keeps on prompting for their name .

name = None

while not name:

    name = input('what is your name ? ')

print('hello '+name)

How does this while loop work , Because as I can understand this while loop checks if the variable name is not null then the condition for the loop is true and keeps on going but how is it not working like that .

If the user does not input anything, input returns '' (empty string) and not '' is True , so in that case the loop would continue.

The print would do well to use an f-string.

print(f"hello {name}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM