Exercise 111 || Solution

Write a Python program that asks the user to enter a text and return him a dictionary whose keys are the words of the text entered and the values are the reverse of the words that make up the text. Example for the text T = "Python is easy", the program must return the dictionary:

d = {'Python': 'nohtyp', 'is': 'si', 'easy': 'ysae'}

Solution

# ask user to type a text
T = input("Type a some text")
# convert the text to list of words
listWords = T.split(' ')
# create an empty dictionary which will contains the words contained in the text
d= dict({})

for word in listWords:
# creating the reverse of words
reverseWord = word[::-1]
d[word] = reverseWord

# display obtained dictionary d
print("The obtained dictionary is d = " , d)
# for typed text = "Python is easy", the output is:
# The obtained dictionary is d = {'Python': 'nohtyP', 'is': 'si', 'easy': 'ysae'}

Younes Derfoufi
my-courses.net

Leave a Reply