Answer the question
In order to leave comments, you need to log in
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()
# dump_words_db.py #!/usr/bin/env python
# -*- coding:utf-8 -*-
import shelve
db=shelve.open('db_file')
print('Yes')
# 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('До встречи')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question