Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51281 > unrolled thread
| Started by | tyler@familyrobbins.com |
|---|---|
| First post | 2013-07-25 22:06 -0700 |
| Last post | 2013-07-26 20:40 -0700 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
Help tyler@familyrobbins.com - 2013-07-25 22:06 -0700
Re: Help alex23 <wuwei23@gmail.com> - 2013-07-26 15:21 +1000
Re: Help Peter Otten <__peter__@web.de> - 2013-07-26 07:27 +0200
Re: Help Chris Angelico <rosuav@gmail.com> - 2013-07-26 06:35 +0100
Re: Help Terry Reedy <tjreedy@udel.edu> - 2013-07-26 01:46 -0400
Re: Help tyler@familyrobbins.com - 2013-07-26 20:40 -0700
| From | tyler@familyrobbins.com |
|---|---|
| Date | 2013-07-25 22:06 -0700 |
| Subject | Help |
| Message-ID | <067018ce-ca0c-415b-a5d3-3c2f79f11e8e@googlegroups.com> |
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.
P.S: I haven't finished the 'search' subroutine, but I just want to get this small problem solved soon so I can
[toc] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-07-26 15:21 +1000 |
| Message-ID | <kst0lp$us6$1@dont-email.me> |
| In reply to | #51281 |
On 26/07/2013 3:06 PM, tyler@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':
...
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-07-26 07:27 +0200 |
| Message-ID | <mailman.5132.1374816489.3114.python-list@python.org> |
| In reply to | #51281 |
tyler@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.
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-07-26 06:35 +0100 |
| Message-ID | <mailman.5133.1374816945.3114.python-list@python.org> |
| In reply to | #51281 |
On Fri, Jul 26, 2013 at 6:06 AM, <tyler@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.
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
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-07-26 01:46 -0400 |
| Message-ID | <mailman.5134.1374817579.3114.python-list@python.org> |
| In reply to | #51281 |
On 7/26/2013 1:06 AM, tyler@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. 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. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | tyler@familyrobbins.com |
|---|---|
| Date | 2013-07-26 20:40 -0700 |
| Message-ID | <084651f1-439d-40e9-b112-a86b739eb151@googlegroups.com> |
| In reply to | #51281 |
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 :).
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web