On 26/07/2013 3:06 PM, ty...@familyrobbins.com wrote:
I'm a bit new to python and I'm trying to create a simple program which adds
words and definitions to a list, and then calls them forward when asked to.
-------------------------------------------------------------------------------
choice = 0
words = []
entry = 0
definition = 0
escape = 0
finish = 0
memory = 0
dictionary = []
search = 0
def ask_ok(prompt, retries=2, complaint='Yes or no, please!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise IOError('refusenik user')
print(complaint)
print("Welcome to Digital Dictionary V1.0!\n\n")
while escape < 1:
choice = input("Type 'Entry' to add a word to the Dictionary, 'Search' to find
a word, and 'Exit' to quit. ")
if choice == '`':
break
if choice == 'Entry' or 'entry':
while finish < 1:
entry = input("Please type the word you wish to add: ")
words.append(entry)
definition = input("What does the word mean?\n ")
dictionary.append(definition)
print(entry, '\n', definition)
ask_ok("Is this entry complete? ")
if True:
finish = 1
entry = 0
definition = 0
elif choice == 'Search' or 'search':
search = input("Please type the word you wish to find: ")
print(search in words)
elif choice == 'Exit' or 'exit':
ask_ok("Are you sure you want to exit? ")
if True:
escape = 1
else:
print("Please choose an option from the list.")
-------------------------------------------------------------------------------
However, if I run the program using anything but 'entry', the program still
runs the 'entry' subroutine. After the 'entry' subroutine is run once, no
options work. Ex:
-------------------------------------------------------------------------------
Welcome to Digital Dictionary V1.0!
Type 'Entry' to add a word to the Dictionary, 'Search' to find a word, and
'Exit' to quit. entry
Please type the word you wish to add: computer
What does the word mean?
a device for computing
computer
a device for computing
Is this entry complete? yes
Type 'Entry' to add a word to the Dictionary, 'Search' to find a word, and
'Exit' to quit. search
Type 'Entry' to add a word to the Dictionary, 'Search' to find a word, and
'Exit' to quit. exit
Type 'Entry' to add a word to the Dictionary, 'Search' to find a word, and
'Exit' to quit. `
-------------------------------------------------------------------------------
The only option which seems to work is the '`' subroutine, which I added to
stop the program after running for a while. I believe it works because it was
added before the 'entry' subroutine.
If anyone can help me figure out why it won't run properly, I'd be really
grateful.
This doesn't do what you think it does:
> if choice == 'Entry' or 'entry':
Python interprets this as:
if (choice == 'Entry') or 'entry':
And as non-empty strings are considered True, it will always succeed and
run the associated code block. The same goes for the subsequent
conditions, but because you break out of the loop in the 'entry'
section, it never reaches them.
You do have it right in your `ask_ok` function, so just rewrite the
conditions in a similar way:
if choice in ('Entry', 'entry'):
Or even better, always make the input lower case and then you only have
one case to test for:
choice = input("Type 'Entry' to etc ... ")
choice = choice.lower()
...
if choice == 'entry':
...
--
http://mail.python.org/mailman/listinfo/python-list