In: Computer Science
Write a function which lets the user enter English words. The user can keep entering English words as long as the user has not entered the word “exit”. Once the user enters “exit” the function will return and print the list of all distinct words starts with English alphabets. Like: A: Ali, apple, … B: Bob, book … until Z. Write a python program for this question. Use main function.
RAW CODE
def wordswrap():
word_list = []
print("Enter Words")
while(1): ### This is infinite loop, runs till breaked or returned
word = input()
if word.lower() == 'exit':
final_dict = {}
for i in set(word_list):
a = i[0] ### First alphabet of word
### Here we are using lower to collect all set of words having capital of small letter in a single alphabet
### Set finds the unique ones from them.
data = set([word.capitalize() for word in word_list if (word.lower()).startswith(a.lower())]) ## Find all words starting with letter
string = ", ".join(data) ### This will make a string from list
final_dict[a.upper()] = string
#final_list.append(string)
return list(sorted(final_dict.items())) ### converting dictionary to list of tuples
word_list.append(word) ## If word is not "exit", append in the list
if __name__ == '__main__': ### This is the main function
list_of_unique_words = wordswrap() ### Calling function.
for alphabet,words in list_of_unique_words:
print(alphabet,": ",words) ### Printing Alphabets and their corresponding words in capitalize form.
SCREENSHOTS (CODE AND OUTPUT)
CODE
OUTPUT
##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####