Utilizing walrus operator for dictionary assignment

Report a typo

You are required to write a program that reads a series of strings from user input until the user enters "stop". Each string is a word. The program should then output a dictionary where the keys are the unique words entered, and the values are the counts of how often each word was entered. Utilize the Walrus Operator (:=) to simplify the code.

The Walrus Operator (:=) can be used to both update and check a value in a dictionary in a single line. Think about how you could use this operator to increment the count of each word in your dictionary as you process the input.

Ensure to utilize the Walrus Operator (:=) in your solution to simplify the code.

Sample Input 1:

apple
banana
apple
stop

Sample Output 1:

Word Counts: {'apple': 2, 'banana': 1}
Write a program in Python 3
word_counts = {}
# Your code here

print(f'Word Counts: {word_counts}')

Create a free account to access the full topic