Help

T

tyler

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.")[/QUOTE][/QUOTE][/QUOTE]
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. `
 
A

alex23

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.")
[/QUOTE]
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. `[/QUOTE]

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':
...
 
P

Peter Otten

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.
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':
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:

The expression

choice == 'Entry' or 'entry'

is evaluated by Python as

(choice == 'Entry') or 'entry'

All strings but "" are True in a boolean context, so this has the same
effect as

(choice == 'Entry') or True

and is always True. Possible fixes:

if choice == "Entry" or choice == "entry":
if choice in ("Entry", "entry"):
if choice.casefold() == "entry".casefold(): # will accept ENTRY, eNtRy etc.
 
C

Chris Angelico

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.

One of the most important tidbits of information is: What version of
Python are you using?
print("Welcome to Digital Dictionary V1.0!\n\n")

This looks like Python 3 style, though it would be valid Python 2 too.
choice = input("Type 'Entry' to add a word to the Dictionary, 'Search' to find a word, and 'Exit' to quit. ")

And I sincerely hope that this is Python 3, otherwise it would be
doing some very strange and dangerous things. However, relying on us
to guess isn't particularly safe, and works only for the major branch
of 2.x vs 3.x. The actual version you're using will be helpful.
while finish < 1:
if True:
finish = 1
entry = 0
definition = 0

You're doing a lot with sentinel values, and it's getting a bit
confusing. Your problem here seems to be that 'finish' is never reset,
so the second time through, your loop is completely skipped. I
recommend instead that you use 'break' to exit your loops.

In a piece of somewhat amusing irony, you're here trying to implement
a dictionary. Python has an object type of that name ('dict' is short
for dictionary), which will be extremely useful here. I advise you to
explore it - it'll replace your words[] and dictionary[] lists.

ChrisA
 
T

Terry Reedy

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.

Why are you not putting them in a Python dict, which is made for this?
Unless you have a very specialized purpose, it would be more educational
about Python programming.
 
T

tyler

Thank you everybody who replied. The problem is fixed now and the program is running correctly. I will also try to use your suggestions in the future to make sure I don't make the same mistake. Thanks again :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top