Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51282

Re: Help

From alex23 <wuwei23@gmail.com>
Newsgroups comp.lang.python
Subject Re: Help
Date 2013-07-26 15:21 +1000
Organization A noiseless patient Spider
Message-ID <kst0lp$us6$1@dont-email.me> (permalink)
References <067018ce-ca0c-415b-a5d3-3c2f79f11e8e@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web