Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Help adding items to a glossary
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.


""" 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')

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':
        print(' you need to enter either s, q or n to continue')
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?
(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

Life of Brian: Conjugate the verb, "to go" !

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Split string into 160-character chunks while adding text to each part iambobbiekings 7 339 May-08-2019, 01:09 AM
Last Post: SheeppOSU
  Find 'greater than' items in list johneven 2 213 Apr-05-2019, 07:22 AM
Last Post: perfringo
  Adding Variable kevinwoo00 4 271 Feb-18-2019, 04:51 PM
Last Post: perfringo
  Finding Row Number for Items in 2D array fafzal 2 323 Jan-10-2019, 06:11 AM
Last Post: fafzal
  How to add items within a list Mrocks22 2 348 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  Adding text to plot Pythcoronas 1 443 Sep-13-2018, 12:27 PM
Last Post: baby_quant
  Adding values to list and pickling mefiak 2 538 May-31-2018, 08:57 AM
Last Post: mefiak
  Checking for items in variable AnOrdinaryFolk 3 767 Nov-26-2017, 04:36 AM
Last Post: heiner55
  number of items per line kiki1113 3 1,326 Nov-19-2017, 04:53 PM
Last Post: nilamo
  adding a number to the list atux_null 4 963 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk

Forum Jump:

Users browsing this thread: 1 Guest(s)