I
I
Ilya Ruchkin2021-10-22 23:08:55
Python
Ilya Ruchkin, 2021-10-22 23:08:55

What is the correct way to use dictionary's update() method?

Hello! I am writing a console dictionary with a function for adding new words (newRecord()) using the update() method. But this function does not add new words to the dictionary (words)! Please tell me what is wrong? There are many examples of using the update() method on the Internet, but they are too simple. Thanks in advance. Sincerely yours, Ilya.

words={'world': 'мир',\
    'earth':'земля',\
    'you': 'ты',\
    'I':'я',\
    'We':'мы',\
    'probably':'вероятно',\
    'piece':'кусок',\
    'tired':'усталый',\
    'should':'должен',\
    'be able':'быть в состоянии',\
    'not enough':'не хватает',\
    'enough':'достаточно',\
    'should':'должен',\
    'represent':'представлять',\
    'sequence':'последовательность'}

def eng():
   eng_words=dict([[v, k] for k,v in words.items()])
   find_word=input('Enter word ' '')
   print(eng_words.get(find_word) or print('No such key'))

def rus():
    key=input('Введите слово ' '')
    print (words.get(key) or 'Искомое слово не найдено')

def newRecord():
    newkey=input('Ввести новое слово ' '')
    newvalue=input('Ввести перевод ' '')
    words.update({newkey: newvalue})
        
if __name__ == '__main__':
    start=input('Найти английский перевод русского слова? введите "y" или "n" ' '')
    if start == 'y':
        eng()
    elif start == 'n':
        rus()
    elif start == 'u':
        newRecord()
    else:
        print('До встречи')

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexander, 2021-10-23
@sanya84

Everything is working.

class SimpleDataBase(dict):
  def __init__(self):
    super().__init__({"World": "Мир"})
  def __str__(self):
    data = "".join(f"{key}: {value}\n" for key, value in self.items())
    return f"{data}"
  
  def update_db(self, key, value):
    self.update({key: value})
    
def main():
    simple_data_base = SimpleDataBase()
    print(simple_data_base) # World: Мир
    simple_data_base.update_db("Red", "Красный")
    print(simple_data_base) # World: Мир
                            # Red: Красный

if __name__ == '__main__':
    main()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question