I
I
Ilya Ruchkin2021-10-10 19:51:27
Python
Ilya Ruchkin, 2021-10-10 19:51:27

Why in the console dictionary is the main file ignoring the database?

Hello! Newbie in programming. I found an interesting (for myself :)) example of a console dictionary ( https://www.unix-lab.org/posts/pydict/ ). The example consists of three files:
1) words_db.py (database):

# words_db.py																						#!/usr/bin/env python											
# -*- coding:utf-8 -*-											
import shelve												
db = shelve.open('db_file')										
db['earth']='земля'											
db['word']='слово'											
db['catch']='ловить'											
db['find']='искать'											
db.close()

2) dump_words_db.py (can't figure out why):
# dump_words_db.py																								#!/usr/bin/env python											
# -*- coding:utf-8 -*-											
import shelve												
db=shelve.open('db_file')										
print('Yes')

3) start_programm.py (main functionality):
# start_programm.py																								#!/usr/bin/env python											
# -*- coding:utf-8 -*-											
import shelve												
db=shelve.open('db_file')
                                               
def eng():
    eng_words=dict([[v, k] for k,v in db.items()])							 
    find_word=input('Enter word ' '')	
    print(eng_words.get(find_word) or print('No such key'))
                                    
def rus():													
    key=input('Введите слово ' '')
    print (db.get(key) or 'Искомое слово не найдено')
    
def newRecord():	
    newkey=input('Ввести новое слово ' '')
    newvalue=input('Ввести перевод ' '')
    db[newkey] = newvalue		
    db.update()												
    db.close()																								

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

The database was created using the shelve module. The program works, but only with words entered after starting the program (by entering "u"). These new words are not entered into the database (words_db.py file). And this database itself (that is, the words placed there when writing the program, and not after it was launched) is generally ignored! For example, the word catch - "catch" is in the database, but the program does not find it either by key or by value. It looks like the code is correct. Where is the mistake? I am using Windows 7 and Python 3.8.1. Thanks in advance! Sincerely yours, Ilya.

61631cdf9860d112215879.png

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
W
Wispik, 2021-10-10
@Wispik

1. Learn how to format a question (the code cannot be inserted as screenshots)
2. There is not enough db in db_file.py. update()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question