Home > Software engineering >  I've built a code for a Simple English to German Dictionary using dict() function in Python, is
I've built a code for a Simple English to German Dictionary using dict() function in Python, is

Time:10-29

Target: I wanted to create an English-German Dictionary which provides two options 1) Add a new word to the dictionary 2) Search for the existing translation in the dictionary

Approach: For this idea, I used the dictionary function in python takes the English word as the Key and the German translation of that word as value to it. Then store it in the text file using the file handle.

Quarry: This is my first try in Python working with File data and dictionary, so I'm
not sure if this coding pattern is correct, as I will be using this idea in my actual project.

The following code works fine but the only issue is that, when I enter new data e.g

In terminal: (1) Add new word (2) Look for the translation ...Type 'done' to exit. ->enter the option:1 Enter English word: one Enter the german version: eins ...takes multiple inputs and the data is saved like this: {'one': 'eins', 'two' : 'zwei', 'three' : 'drei'...}

Issue: Now when I try to use Option 2: Enter the word to get german translation: one -> and I get the following output enter image description here

eng2ger = dict()

#Function:
def eng_ger_dict(f_name):
    i = input("(1) Add new word\n(2) Look for the translation\n...Type 'done' to exit.\n->enter the option:")
    x = 0
    i = i.strip().lower()
    if not i == 'done':
        if i == 1 or 2:        
            inp = int(i)
            #Option 1: Writting new word in dictionary
            if inp == 1:
                #input from user
                eng = str(input("Enter english word: "))
                ger = str(input("Enter german version: "))

                #creating dictionary
                eng2ger[eng] = ger
                print(eng2ger, "\n")

                #opening text file
                f_write = open(f_name,"w")
                line = str(eng2ger)
                f_write.write(line)
                eng_ger_dict(f_name) 

            #Option 2: Searching for the word
            elif inp == 2:
                f_read = open(f_name)
                new_dict = dict()
                new_dict = f_read
                word = str(input("Enter the english word to get the german version of it: "))
                
                
                for lines in new_dict:
                    
                    lines = dict()
                    lines = lines
                    if lines.get(word) == -1:
                        continue
                    else:
                        #I also tried to get output from string slicing 
                        # com_pos = lines.find(",")
                        # col_pos = lines.find(":")
                        # lines.split(com)
                        # pos = lines.find[new_word]
                        # print(new_word[pos : com_pos],"\n")
                        # eng_ger_dict("eng2ger.txt")

                        print(lines.get(word))              
                else:
                    print("German version of", word, "does not exist in dictionary,              
                         'you can add new word by using 1st option :)\n")
                    eng_ger_dict("eng2ger.txt")
                    
        else:
            print("Please select the option 1 or 2, else type done to exit\n")
    else:
        f_name.close()
        exit()

#Function call:
eng_ger_dict("eng2ger.txt")

CodePudding user response:

It seems that the question is in two parts:

  1. searching for a word.
  2. adding a word.

Searching for an item is a dict is relatively straight forward, like this:

d = {'one': 'eins', 'two' : 'zwei', 'three' : 'drei'}

word = 'one'

if word in d.keys():
    print('the word is here')

adding a word is done like this:

d = {'one': 'eins', 'two' : 'zwei', 'three' : 'drei'}

new_word_english = 'four'
new_word_german = 'vier'

d['four'] = 'vier'

which gives this:

{'one': 'eins', 'two': 'zwei', 'three': 'drei', 'four': 'vier'}

You would have to implement both of the above into the routines accordingly.

  • Related