Input a sentence from the user and count the frequency of each word using a dictionary. sentence = input("Enter a sentence: ")
words = sentence.split()
frequency = {}
for word in words:
if word in frequency:
frequency[word] += 1
else:
frequency[word] = 1
print("\nWord Frequency:")
for word in frequency:
print(f"{word}: {frequency[word]}")
sentence = input("Enter a sentence: ")
words = sentence.split()
frequency = {}
for word in words:
if word in frequency:
frequency[word] += 1
else:
frequency[word] = 1
print("\nWord Frequency:")
for word in frequency:
print(f"{word}: {frequency[word]}")
Output:
Enter a sentence: hello iam here
Word Frequency:
hello: 1
iam: 1
here: 1
Process finished with exit code 0