简体   繁体   中英

Using a blank line as an indicator value to end the input in while loop

I am still new to Python. My professor gave us a lab activity that needs to print words in a list from the user's input that does not duplicate its elements which are only displayed in one occurence. Everything works well except I am not quite sure by what our professor means by having a "blank line" as an indicator value to terminate the looping input.

word_Box = []                 

enter_Word = str(input("\nEnter a word. (Press 'Enter' key if finished.): \n")) #asking for user's input'

while enter_Word != "": #asks for input until user does not have input/pressing Enter key in keyboard
        word_Box.append(enter_Word) #stores every inputted word at the end of the list
        enter_Word = str(input("\nEnter a word (Press 'Enter' key if finished.): \n"))

word_Box = list(dict.fromkeys(word_Box)) #elements of the list is converted to keys of a dictionary to remove duplicates as dictionaries don't allow duplicates
                                                                         #dictionary is converted back to a list

print("\nThe word/s you entered is/are: ", word_Box)   #prints all the elements                                                       

When this line is reached inside the loop

enter_Word = str(input("\nEnter a word (Press 'Enter' key if finished.): \n"))

If the enter key is pressed then the variable enter_Word = “”

Because that is the exit condition of your loop the loop will stop and the program will continue onto the line

word_Box = list(dict.fromkeys(word_Box))

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