clk
clk

Reputation: 3

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                                                       

Upvotes: 0

Views: 436

Answers (1)

KILLtheWEEZEL
KILLtheWEEZEL

Reputation: 268

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))

Upvotes: 1

Related Questions