Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

write a python program that prompts the user to enter a list of words a…

Question

write a python program that prompts the user to enter a list of words and checks whether the list contains only unique elements. if not, print the duplicate words.

Explanation:

Step1: Get user - input list

word_list = input("Enter a list of words separated by spaces: ").split()

Step2: Create a dictionary to count word occurrences

word_count = {}
for word in word_list:
    if word in word_count:
        word_count[word] += 1
    else:
        word_count[word] = 1

Step3: Check for duplicates and print

duplicates = []
for word, count in word_count.items():
    if count > 1:
        duplicates.append(word)
if duplicates:
    print("Duplicate words:", duplicates)
else:
    print("The list contains only unique elements.")

Answer:

The above Python code is the solution.