Python Dictionary

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Python Dictionary

Post 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.
Last edited by lotios611 on Sat Dec 05, 2009 8:34 am, edited 1 time in total.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
Big Grizzle
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 61
Joined: Wed Sep 02, 2009 1:07 pm
Current Project: 2d side scrolling shooter.
Favorite Gaming Platforms: The SNES rules all!!! Super Aleste baby!
Programming Language of Choice: Python
Location: London, UK
Contact:

Re: Python Dictionary

Post 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.
"Simplicity is the ultimate sophistication." - Leonardo da Vinci
http://caveofgrizzle.blogspot.com/
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Python Dictionary

Post 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.
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Python Dictionary

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Python Dictionary

Post by lotios611 »

Scoody wrote:

Code: Select all

if answer.lower() in ['a','a)']:
Thanks for the tip, I'll add that.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
Post Reply