How to code a series of alternatives

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott F

    How to code a series of alternatives

    testvar = bob

    # Trial A: This does not work
    if testvar == ("fred" or "bob):
    statements

    # Trial B: This does work
    if textvar == "fred" or textvar == "bob":
    statements


    Trial B is OK for 1 or 2 tests, but gets messy for 5 or 6. What is the
    variation of Trial A that will work?


    Scott
  • Aahz

    #2
    Re: How to code a series of alternatives

    In article <Xns94987ED355A 3Asdfexpertunec om@216.168.3.44 >,
    Scott F <sdfATexpertune DOTcom> wrote:[color=blue]
    >testvar = bob
    >
    ># Trial A: This does not work
    >if testvar == ("fred" or "bob):
    > statements
    >
    ># Trial B: This does work
    >if textvar == "fred" or textvar == "bob":
    > statements
    >
    >Trial B is OK for 1 or 2 tests, but gets messy for 5 or 6. What is the
    >variation of Trial A that will work?[/color]

    Simplest:

    if testvar in ('fred', 'bob'):

    Fastest:

    options = sets.Set('fred' , 'bob'):

    ...

    if testvar in options:
    --
    Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

    "Do not taunt happy fun for loops. Do not change lists you are looping over."
    --Remco Gerlich, comp.lang.pytho n

    Comment

    • John Burton

      #3
      Re: How to code a series of alternatives

      Scott F wrote:[color=blue]
      > testvar = bob
      >
      > # Trial A: This does not work
      > if testvar == ("fred" or "bob):
      > statements
      >
      > # Trial B: This does work
      > if textvar == "fred" or textvar == "bob":
      > statements
      >
      >
      > Trial B is OK for 1 or 2 tests, but gets messy for 5 or 6. What is the
      > variation of Trial A that will work?[/color]

      if testvar in ["fred", "bob"]:
      statements

      Comment

      • Scott F

        #4
        Re: How to code a series of alternatives

        aahz@pythoncraf t.com (Aahz) wrote in
        news:c1dhgu$orm $1@panix3.panix .com:
        [color=blue]
        >
        > Simplest:
        >
        > if testvar in ('fred', 'bob'):
        >
        > Fastest:
        >
        > options = sets.Set('fred' , 'bob'):
        >
        > ...
        >
        > if testvar in options:[/color]


        Thank you. I'd be embarassed, but I'm only grateful.

        Scott

        Comment

        • F. Petitjean

          #5
          Re: How to code a series of alternatives

          On Mon, 23 Feb 2004 18:28:03 -0000, Scott F <> wrote:[color=blue]
          > testvar = bob[/color]
          I suppose you mean : testvar = "bob"
          [color=blue]
          > # Trial A: This does not work
          > if testvar == ("fred" or "bob):
          > statements[/color]
          How about this ?
          if testvar in ("fred", bob"):
          statements[color=blue]
          >
          > # Trial B: This does work
          > if textvar == "fred" or textvar == "bob":
          > statements
          >
          >
          > Trial B is OK for 1 or 2 tests, but gets messy for 5 or 6. What is the
          > variation of Trial A that will work?[/color]

          alternatives = ("one", "two", "three")
          if alternative in alternatives:
          # statements


          Regards

          Comment

          • Paul Rubin

            #6
            Re: How to code a series of alternatives

            Scott F <sdfATexpertune DOTcom> writes:
            [color=blue]
            > testvar = bob
            >
            > # Trial A: This does not work
            > if testvar == ("fred" or "bob):
            > statements
            >
            > # Trial B: This does work
            > if textvar == "fred" or textvar == "bob":
            > statements
            >
            > Trial B is OK for 1 or 2 tests, but gets messy for 5 or 6. What is the
            > variation of Trial A that will work?[/color]

            if testvar in ("fred","bob "):
            statements

            Comment

            Working...