Page 1 of 1

Python Dictionary

Posted: Fri Dec 04, 2009 6:08 pm
by lotios611
I've been coding a dictionary using Python. Any one have any suggestions? Here's the code:

Code: Select all

import json
dictionary = {}
f = open("Dictionary.txt", "w")

if f.readline() == "" :
    f.write("{}")
    
f.close();
exitLoop = False

while exitLoop == False :
    dictionary = json.loads(open("Dictionary.txt").read())
    print("Welcome to the dictionary.")
    print("To begin, type your option.")
    print("a) Search for a word")
    print("b) Enter a word")
    print("c) Display all words")
    answer = raw_input()

    if answer.lower() in ['a', 'a)'] :
        word = raw_input("What word would you like to search for? ")
        if word in dictionary :
            print(word.capitalize()+" - "+dictionary[word].capitalize())
        else :
            print("Word not found!")

    elif answer.lower() in ['b', 'b)'] :
        word = raw_input("Enter a word: ")
        definition = raw_input("Enter the definition: ")
        word2 = word.lower();
        definition2 = definition.lower();
        dictionary[word2] = definition2
        json.dump(dictionary, open("Dictionary.txt", "w"))

    elif answer.lower() in ['c', 'c)'] :
        for k, v in dictionary.iteritems():
            print (k.capitalize()+" - "+v.capitalize())

    print("Would you like to quit? Y/N")
    wantToQuit = raw_input();

    if wantToQuit == "y" or wantToQuit == "Y" :
        exitLoop = True

    else :
        exitLoop = False
EDIT: Made it so you don't have to create a file called Dictionary containing {}.
EDIT2: Cleaned up code.

Re: Python Dictionary

Posted: Fri Dec 04, 2009 8:16 pm
by Big Grizzle
I don't have the json Python library so can't run the code.

I do have a tip to make your code more readable and succinct. Instead of...

Code: Select all

if answer == "a" or answer == "A" or answer == "a)" or answer == "A)": 
...you could use...

Code: Select all

if answer.lower()[0] == "a":
By calling lower() on the variable answer and then using indexing to specify the first char in the string, you safeguard yourself against the user having the caps-lock on and/or them adding on an unnecessary bracket.

Re: Python Dictionary

Posted: Sat Dec 05, 2009 1:27 am
by Scoody
Big Grizzle wrote:I don't have the json Python library so can't run the code.

I do have a tip to make your code more readable and succinct. Instead of...

Code: Select all

if answer == "a" or answer == "A" or answer == "a)" or answer == "A)": 
...you could use...

Code: Select all

if answer.lower()[0] == "a":
By calling lower() on the variable answer and then using indexing to specify the first char in the string, you safeguard yourself against the user having the caps-lock on and/or them adding on an unnecessary bracket.
Depending on the situation this could be a bad thing. If you have a "critical" decision, and the user enters "yes", changes his mind and only erases "es", then gets "yno". So if you're checking for "y" ...
Another alternative:

Code: Select all

if answer.lower() in ['a','a)']:

But if my "situation" isn't a problem either way will do.

Re: Python Dictionary

Posted: Sat Dec 05, 2009 8:23 am
by lotios611
Big Grizzle wrote:I don't have the json Python library so can't run the code.
I used json because I wanted it to be easy to directly edit the text file. But if you have another solution, please tell me.

Re: Python Dictionary

Posted: Sat Dec 05, 2009 8:25 am
by lotios611
Scoody wrote:

Code: Select all

if answer.lower() in ['a','a)']:
Thanks for the tip, I'll add that.