Answer:
The program in Python is as follows:
fname = input("Enter the translation file name: ")
with open(fname) as file_in:
lines = []
for line in file_in:
lines.append(line.rstrip('\n'))
myDict = {}
for i in range(len(lines)):
x = lines[i].split(":")
myDict[x[0].lower()] = x[1].lower()
print("Enter an English word to receive the Spanish translation.\nPress ENTER to quit.")
word = input("Enter an English word: ")
while(True):
if not word:
break
if word.lower() in myDict:
print("The Spanish word is ",myDict[word.lower()])
else:
print("I don’t have that word in my list.")
word = input("Enter an English word: ")
Explanation:
This prompts the user for file name
fname = input("Enter the translation file name: ")
This opens the file for read operation
with open(fname) as file_in:
This creates an empty list
lines = []
This reads through the lines of the file
for line in file_in:
This appends each line as an element of the list
lines.append(line.rstrip('\n'))
This creates an empty dictionaty
myDict = {}
This iterates through the list
for i in range(len(lines)):
This splits each list element by :
x = lines[i].split(":")
This populates the dictionary with the list elements
myDict[x[0].lower()] = x[1].lower()
This prints an instruction on how to use the program
print("Enter an English word to receive the Spanish translation.\nPress ENTER to quit.")
This prompts the user for an English word
word = input("Enter an English word: ")
This loop is repeated until the user presses the ENTER key
while(True):
If user presses the ENTER key
if not word:
The loop is exited
break
If otherwise, this checks if the word exists in the dictionary
if word.lower() in myDict:
If yes, this prints the Spanish translation
print("The Spanish word is ",myDict[word.lower()])
If otherwise,
else:
Print word does not exist
print("I don’t have that word in my list.")
Prompt the user for another word
word = input("Enter an English word: ")