noob question Letters in words?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Shevanski

    #1

    noob question Letters in words?

    Alright heres another noob question for everyone. Alright, say I have a
    menu like this.

    print "1. . .Start"
    print "2. . .End"
    choice1 = raw_input("> ")

    and then I had this to determine what option.


    if choice1 in ('1', 'Start', 'start'):
    #do first option
    if choice1 in ('2', 'End', 'end'):
    #do second option

    Is there a way (I searched for a module but didnt find one) that I can do
    something like this?

    if choice1 in ('1', 'S', 's'):
    #do first option
    if choice1 in ('2', 'E', 'e'):
    #do second option


    For instance I could type in Stop and would get the first option since it
    had an "s" in it?
    Anyone heard of any way to do this?

    Thanks,
    -Ivan



    By the way, if anyone gets tired of my persistant noob questions please tell
    me I don't want to bother anyone =D

    _______________ _______________ _______________ _______________ _____
    Express yourself instantly with MSN Messenger! Download today - it's FREE!


  • mensanator@aol.com

    #2
    Re: noob question Letters in words?


    Ivan Shevanski wrote:[color=blue]
    > Alright heres another noob question for everyone. Alright, say I have a
    > menu like this.
    >
    > print "1. . .Start"
    > print "2. . .End"
    > choice1 = raw_input("> ")
    >
    > and then I had this to determine what option.
    >
    >
    > if choice1 in ('1', 'Start', 'start'):
    > #do first option
    > if choice1 in ('2', 'End', 'end'):
    > #do second option
    >
    > Is there a way (I searched for a module but didnt find one) that I can do
    > something like this?
    >
    > if choice1 in ('1', 'S', 's'):
    > #do first option
    > if choice1 in ('2', 'E', 'e'):
    > #do second option[/color]

    Why not just look at the first letter the user types instead of
    the whole string?

    if choice1[0] in ('1', 'S', 's'):
    #do first option
    if choice1[0] in ('2', 'E', 'e'):
    #do second option

    [color=blue]
    >
    >
    > For instance I could type in Stop and would get the first option since it
    > had an "s" in it?
    > Anyone heard of any way to do this?
    >
    > Thanks,
    > -Ivan
    >
    >
    >
    > By the way, if anyone gets tired of my persistant noob questions please tell
    > me I don't want to bother anyone =D
    >
    > _______________ _______________ _______________ _______________ _____
    > Express yourself instantly with MSN Messenger! Download today - it's FREE!
    > http://messenger.msn.click-url.com/g...ave/direct/01/[/color]

    Comment

    • Rob Cowie

      #3
      Re: noob question Letters in words?

      A string can be thought of as a tuple of characters. Tuples support
      membership testing thus...

      choice1 = raw_input("> ")

      if '1' or 's' or 'S' in choice1:
      #do something
      elif '2' or 'e' or E' in choice1:
      #do something

      It doesn't seem to me to be a good idea; If the input is 'Start',
      option1 is executed, likewise if the input is 'Stop', or any other
      string with 's' in it.

      Perhaps a better idea is to present the user with a choice that cannot
      be deviated from, along the lines of...

      def main():
      print "1.\tStart"
      print "2.\tSometh ing Else"
      print "3.\tStop"

      x = raw_input()
      if x is '1': print 'Start'
      elif x is '2': print 'Something else'
      elif x is '3': print 'End'
      else: main()

      Comment

      • Steven D'Aprano

        #4
        Re: noob question Letters in words?

        On Fri, 07 Oct 2005 20:46:39 -0400, Ivan Shevanski wrote:
        [color=blue]
        > Alright heres another noob question for everyone. Alright, say I have a
        > menu like this.
        >
        > print "1. . .Start"
        > print "2. . .End"
        > choice1 = raw_input("> ")
        >
        > and then I had this to determine what option.[/color]

        Firstly, you need to decide on what you want to accept. If you really do
        want to (I quote from later in your post)
        [color=blue]
        > For instance I could type in Stop and would get the first option since
        > it had an "s" in it?[/color]

        then you can, but that would be a BAD idea!!!

        py> run_dangerous_c ode()
        WARNING! This will erase all your data if you continue!
        Are you sure you want to start? Yes/Start/Begin[color=blue]
        > Stop[/color]
        Starting now... data erased.

        Oh yeah, your users will love you for that.

        I could handle it like this:

        def yesno(s):
        """Returns a three-valued flag based on string s.
        The flag is 1 for "go ahead", 0 for "don't go"
        and -1 for any other unrecognised response.
        Recognises lots of different synonyms for yes and no.
        """
        s = s.strip().lower () # case and whitespace doesn't matter
        if s in ('1', 'start', 's', 'begin', 'ok', 'okay', 'yes'):
        return 1
        elif s in ('2', 'end', 'e', 'cancel', 'c', 'stop', 'no'):
        return 0
        else:
        return -1


        def query():
        print "Start process: 1 start"
        print "End process: 2 end"
        raw_choice = raw_input("> ")
        choice = yesno(raw_choic e)
        if choice == -1:
        print "I'm sorry, I don't understand your response."
        return query()
        else:
        return choice


        But that's probably confusing. Why do you need to offer so many ways of
        answering the question? Generally, the best way of doing this sort of
        thing is:

        - give only one way of saying "go ahead", which is usually "yes".
        - give only one way of saying "don't go ahead", which is usually "no".
        - if the thing you are doing is not dangerous, allow a simple return to
        do the same as the default.
        - if, and only if, the two choices are unambiguous, allow the first
        letter on its own (e.g. "y" or "n") to mean the same as the entire word.

        Using this simpler method:

        def yesno(s):
        s = s.strip().lower ()
        if s in ('yes', 'y'):
        return 1
        elif s in ('no', 'n'):
        return 0
        else:
        return -1

        def query:
        print "Start process? (y/n)"
        choice = yesno(raw_choic e("> "))
        if choice == -1:
        print "Please type Yes or No."
        return query()
        else:
        return choice


        Hope this is helpful.


        --
        Steven.

        Comment

        • Paul Rubin

          #5
          Re: noob question Letters in words?

          "Ivan Shevanski" <darkpaladin79@ hotmail.com> writes:[color=blue]
          > choice1 = raw_input("> ")[/color]

          choice1 is now the whole string that the user types
          [color=blue]
          > Is there a way (I searched for a module but didnt find one) that I can
          > do something like this?
          >
          > if choice1 in ('1', 'S', 's'):
          > #do first option[/color]

          You'd use choice1[0] to get the first letter of choice1.

          Comment

          • Steven D'Aprano

            #6
            Re: noob question Letters in words?

            On Fri, 07 Oct 2005 18:03:02 -0700, mensanator@aol. com wrote:
            [color=blue]
            > Why not just look at the first letter the user types instead of
            > the whole string?
            >
            > if choice1[0] in ('1', 'S', 's'):
            > #do first option
            > if choice1[0] in ('2', 'E', 'e'):
            > #do second option[/color]

            Is it *really* a good idea if the user types "STOP!!!" and it has the
            same effect as if they typed "Start please"?

            I've heard of "Do what I mean, not what I said" systems, which are usually
            a really bad idea, but this is the first time I've seen a "Do what I don't
            mean, not what I said" system.



            --
            Steven.

            Comment

            • Fredrik Lundh

              #7
              Re: noob question Letters in words?

              (re that earlier thread, I think that everyone that thinks that it's a
              good thing that certain Python constructs makes grammatical sense
              in english should read the previous post carefully...)

              Rob Cowie wrote:
              [color=blue]
              > A string can be thought of as a tuple of characters.[/color]

              footnote: the correct python term is "sequence of characters".
              [color=blue]
              > Tuples support membership testing thus...
              >
              > choice1 = raw_input("> ")
              >
              > if '1' or 's' or 'S' in choice1:
              > #do something
              > elif '2' or 'e' or E' in choice1:
              > #do something
              >
              > It doesn't seem to me to be a good idea; If the input is 'Start',
              > option1 is executed, likewise if the input is 'Stop', or any other
              > string with 's' in it.[/color]

              in fact, the first choice is always executed, because

              if '1' or 's' or 'S' in choice1:
              ...

              might look as if it meant

              if ('1' in choice1) or ('s' in choice1) or ('S' in choice1):
              ...

              but it really means

              if ('1') or ('s') or ('S' in choice1):
              ...

              since non-empty strings are true, the '1' will make sure that the entire
              expression is always true, no matter what choice1 contains.
              [color=blue]
              > Perhaps a better idea is to present the user with a choice that cannot
              > be deviated from, along the lines of...
              >
              > def main():
              > print "1.\tStart"
              > print "2.\tSometh ing Else"
              > print "3.\tStop"
              >
              > x = raw_input()
              > if x is '1': print 'Start'
              > elif x is '2': print 'Something else'
              > elif x is '3': print 'End'
              > else: main()[/color]

              The "is" operator tests for object identity, not equivalence. Nothing
              stops a Python implementation from creating *different* string objects
              with the same contents, so the above isn't guaranteed to work. (it
              does work on current CPython versions, but that's an implementation
              optimization, and not something you can rely on).

              You should always use "==" to test for equivalence (and you should never
              use "is" unless you know exactly what you're doing).

              </F>



              Comment

              • Rob Cowie

                #8
                Re: noob question Letters in words?

                Well.. that put me in my place!

                Fredrik Lundh - I hadn't realised that 'is' does not test for
                equivalence. Thanks for the advice.

                Comment

                • Clint Norton

                  #9
                  Re: noob question Letters in words?

                  BTW do yourself a favor and learn to use the cmd module:

                  2 nice examples:


                  it will also give you command completion if your termianl supports it
                  (most terminals on most linux distros do).

                  Comment

                  Working...