Python Forum
Help adding items to a glossary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help adding items to a glossary
#1
hey folks, I have a question for my homework where I need to add words and a definition to a glossary then make a user aware that the word/definition has been added to the glossary file so far my program works as it should, it asks the user to press a certain key to see a word from the glossary that gets imported from the csv thing then prompts the user to press enter to see the definition of the word, and also has the option to stop the program, i need to make another definition to add more but I'm so lost trying to do it can anyone help with this? code I have is below, if possible can any help be kept as low level as possible so I can understand it fully? thanks in advance people.

 # Revision_helper.py

""" this flashcard program will allow the user to prompt for a glossary index,
it will show the user one of the indexes and when the user presses return
will give a definition of the word that they can use as study material"""



from random import *
import csv 



def show_flashcard():
    """ show the user a random key and ask them the definition,
        show the correct definition once the enter key is pressed"""

    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('press return to see the correct definition')
    print(glossary[random_key])

    
def file_to_dictionary(filename):
    """ create a dictionary with the contents of a file"""
    
    file = open(filename, 'r')
    reader = csv.reader(file)
    dictionary = {}
    for row in reader:
        dictionary[row[0]] = row[1]
    return dictionary



# setup the glossary
glossary = file_to_dictionary('Glossary.txt')



# The interactive loop
exit = False
while not exit:
    user_input = input(' Enter s to show a flashcard and q to quit or n to add a new entry' )
    if user_input == 'q':
        exit = True
    elif user_input == 's':
        show_flashcard()
    else:
        print(' you need to enter either s, q or n to continue')
Reply
#2
So far, your script looks good.

There are a couple things I would change. In show_flashcard(), there are two issues. First, it does not take an argument which means that glossary is a global variable. Using global variables like that is a bad practice. Anything referenced inside a function should be local - either passed in as an argument or defined within the function. Changing the function signature to show_flashcard(glossary) will improve it.

The second issue with it is only line 18. While it technically works, I would use dict.keys() instead of list(dict). Probably just a style difference.

For your inputs, I recommend calling str.lower(), str.upper(), or something equivalent. That way, your comparisons permit for uppercase and lowercase inputs from the user.

As for your actual question... I suppose adding to the dictionary is the obstacle. You can call the dict.update() method to insert new keys and values. For your project, I recommend that your function checks for the new key before requesting the definition. That way, you can ensure that the user is not overwriting definitions that have already been entered. Does that help?
Reply
#3
(Jan-08-2019, 03:31 AM)stullis Wrote: I would use dict.keys() instead of list(dict). Probably just a style difference.

In this specific example it's style difference. However, there is more to that. You can't iterate over dict.keys() and modify the dictionary at the same time, but with list(dict) you can:

>>> d = {k: k for k in range(4)}
>>> d
{0: 0, 1: 1, 2: 2, 3: 3}
>>> for k in d.keys():
...     del d[k]
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
>>> d
{1: 1, 2: 2, 3: 3}        # deleted first key and raised RuntimeError
>>> for k in list(d):
...     del d[k]
...
>>> d
{}                        # deleted all keys without error
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020