HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Practice with dictionaries

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
practicedictionarieswith

Problem

Post a cursory read of this I implemented a simple dictionary and an interface to assign, replace, look up, and redefine its terms to apply the concepts.

It's simple, but I'd still like to know any way I could make this cleaner and more pythonic.

```
def print_dict(dictionary):
for key in dictionary:
print("{} : {}".format(key, dictionary[key]))

def display_menu():
print("\n0 = Quit"
+ "\n1 = Look up a term"
+ "\n2 = Add a term"
+ "\n3 = Redefine a term"
+ "\n4 = Delete a term"
+ "\n5 = Display Dictionary"
)

def is_integer(value):
try:
temp = int(value)
return True
except ValueError:
return False

def validate(choice):
if is_integer(choice) and 0 <= int(choice) <= 5:
return int(choice)
else:
print("Input must be an integer between 0 and 5, inclusive")
return validate(input("\nEnter Selection: "))

def lookup_term(dictionary):
term = input("which term would you like to look up? ")
if term in dictionary:
print("{} : {}".format(term, dictionary.get(term)))
else:
print("Term does not exist, input 2 to add new term")

def redefine_term(dictionary):
term = input("which term would you like to redefine? ")
if term in dictionary:
dictionary[term] = input("and its definition? ")
else:
print("Term does not exist, input 2 to add new term")

def add_term(dictionary):
term = input("What term would you like to add? ")
if term in dictionary:
print("Already exists. To redfine input 3")
else:
dictionary[term] = input("and its definition? ")

def delete_term(dictionary):
del dictionary[input('Which term would you like to delete? ')]

def process_request(choice, dictionary):
if choice == 0:
print("Thank you for using Stack Exchange Site Abbreviation!")
quit()
elif choice == 1:
lookup_term(dictionary)
elif choice == 2:
add_term(dictionary)
elif choice == 3:
redefine_term(dictionary)
elif choice == 4:
delete_term(dictionary)
else:
print_dict(

Solution

Some small improvements:

-
print_dict method: use iteritems()

for key, value in dictionary.iteritems():
print("{} : {}".format(key, value))


-
You can merge validate and is_integer. There are no benefits here gained by separating them in two different functions.

-
For lookup_term, use either:

value = dictionary.get(term)
if value:
    print("{} : {}".format(term, value))
else:
    print("Term does not exist, input 2 to add new term")


Or you could use:

if term in dictionary:
    print("{} : {}".format(term, dictionary[term]))
else:
    print("Term does not exist, input 2 to add new term")


The point I wanted to illustrate was use of dict.get()

-
In delete_term, you are not validating for input when key is not in dictionary.

Some other things:

Since all functions work with same dictionary, they could have been grouped together under same class with dictionary being shared data member.

Also, your dictionary is case sensitive. Looking at your use-case, this is not something you want.

If at anytime you need to change menu, you would need to change validate function. Maybe there should be easier way to do this? (Instead of else denoting 5 in menu, else could work for something else?)

Code Snippets

for key, value in dictionary.iteritems():
print("{} : {}".format(key, value))
value = dictionary.get(term)
if value:
    print("{} : {}".format(term, value))
else:
    print("Term does not exist, input 2 to add new term")
if term in dictionary:
    print("{} : {}".format(term, dictionary[term]))
else:
    print("Term does not exist, input 2 to add new term")

Context

StackExchange Code Review Q#110412, answer score: 2

Revisions (0)

No revisions yet.