Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#example of using a dictionary to store words and translations from tkinter impo

ID: 3595413 • Letter: #

Question

#example of using a dictionary to store words and translations
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir = "/",
title = "Select file",
filetypes = (("txt files","*.txt"),("all files","*.*")))
# This will get a file path and name.
print (root.filename)
infile = open(root.filename, "r") #This will open a file with “read” only.

str=infile.read() #This will read all content then save it to a variable ‘str’
print(str) # This will print the content of the file.

#str=infile.read() (1) Make coment this line
#print(str) (2) Make coment this line
# (3) Add from below lines for “TranslationDictionary.py (2)
translationDictionary = dict() # prepare an empty dictionary
print("Dictionary is prepareing! Would you wait!")
#i=1 # for trace, we will make this as comment later.
try: # try below
for line in infile.readlines(): # This will read each line
line = line.strip() # get rid of newline charactes
index = line.find('=') # find the position of = sign,
wordOne = line[0:index] # separate each word_1.
wordTwo = line[index+1:] # separate each word_2.
wordOne = wordOne.strip(' ') # get rid of extra spaces
wordTwo = wordTwo.strip(' ') # get rid of extra spaces
translationDictionary[wordOne] = wordTwo #store in dictionary
#print(i) # trace each line is completed.
#i=i+1
except: # in case of exception
print(root.filename + " can not be processed")
finally: # once you done try,
infile.close() # close file
print("done!")
items = translationDictionary.items() # put all items in a list items
for item in items: # list loop
print(item) # print each

Requirement 1. Get an English Word for translation from user input. 2_1. If there is no user input English word (as key) in the dictionary, > display some message for that. 2 2. If user input English word (as key) exists, -> display translated word (a value of the dictionary). 3. Step 1 and 2 should be inside loop. If a user choose to exit, then, display Bye words. RESTART: C:/Users/kuk/Desktop/CIT 253/files/Modul C:/Users/kuk/Desktop/CIT_253/files/Module_7_Dictio Dictionary is prepareing! Please, wait for me! done! Enter an English word (x to exit): hello hello is not in the dictionary. Try other words. Enter a English word (x to exit): test test translates to provi . Enter a English word (x to exit): x Thank you for using our dictionary. Bye!

Explanation / Answer

#example of using a dictionary to store words and translations
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("txt files","*.txt"),("all files","*.*")))
# This will get a file path and name.
print (root.filename)
infile = open(root.filename, "r") #This will open a file with ?read? only.
#str=infile.read() #This will read all content then save it to a variable ?str?
#print(str) # This will print the content of the file.
#str=infile.read() (1) Make coment this line
#print(str) (2) Make coment this line
# (3) Add from below lines for ?TranslationDictionary.py (2)
translationDictionary = dict() # prepare an empty dictionary
print("Dictionary is prepareing! Would you wait!")
#i=1 # for trace, we will make this as comment later.
try: # try below
for line in infile.readlines(): # This will read each line
line = line.strip() # get rid of newline charactes
index = line.find('=') # find the position of = sign,
wordOne = line[0:index] # separate each word_1.
wordTwo = line[index+1:] # separate each word_2.
wordOne = wordOne.strip(' ') # get rid of extra spaces
wordTwo = wordTwo.strip(' ') # get rid of extra spaces
translationDictionary[wordOne] = wordTwo #store in dictionary
#print(i) # trace each line is completed.
#i=i+1
except: # in case of exception
print(root.filename + " can not be processed")
finally: # once you done try,
infile.close() # close file
print("done!")

while True:
englishWord = input("Enter an English word(x toexit): ")
if englishWord == "x":
break
if englishWord in translationDictionary:
print(englishWord, "translates to ", translationDictionary[englishWord])
else:
print(englishWord, "is not in the dictionary. Try other words.")

print("Thank you for using our dictionary. Bye!")

# copy pastable code link: https://paste.ee/p/SEjZD