Python control statements [SOLVED]
Posted: Sun Nov 29, 2009 11:44 am
I'm trying to make a dictionary. When I run this code, I get prompted to enter my option. If I enter b, it runs the code for c.
EDIT: I figured it out. I had "if answer == "b" or "b)" It should of been "if answer == "b" or answer == "b)".
Code: Select all
dictionary={"Hello" : "A word", "Hi" : "Another word", "Yo!" : "Yet another word"}
print("Welcome to the dictionary.\n")
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 == "a" or "a)" or "A" or "A)" :
word = raw_input("What word would you like to search for? ")
if word in dictionary :
print(dictionary[word])
else :
print("Word not found!")
elif answer == "b" or "b)" or "B" or "B)" :
word = raw_input("Enter a word: ")
definition = raw_input("Enter the definition: ")
dictionary[word] = definition
elif answer == "c" or "c)" or "C" or "C)" :
for k, v in dictionary.iteritems() :
print (k+" - "+v)