Python Course 11: Dictionaries and REPLs

Copy the following code into your IDE

terms = {"bit":"binary digit", "byte":"8 Bits", "kilobyte":"1000 Bytes"}

def Main():
    # The input output loop
    ans = ""
    while ans != "end":
	print("L to Look up a Word")
	print("E to Enter new Word")
	print("end to exit the program")

	ans = input("What do you want to do?")

	if ans == "L":
	    lookup()

	elif ans == "E":
	    enter()

	else:
	    print("I don't understand that command")


def lookup():
    word = input("Enter the word you wish to look up")
    if word in terms:
	print(terms.get(word))
    else:
	print("Word not in dictionary")


def enter():
    word = input("Enter new word")
    definition = input("Enter the definition")
    terms[word] = definition


Main()

Revision Helper Exercise

  1. Run the above code. Make sure you understand how it works
  2. Follow the link to see Computing Key Terms. Add some of these to your dictionary.
  3. Add an option to the Main() loop to print out all the key values in the terms dictionary. Implement that option

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s