How to code a series of alternatives

S

Scott F

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
 
J

John Burton

Scott said:
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?

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

Aahz

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?

Simplest:

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

Fastest:

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

...

if testvar in options:
 
S

Scott F

(e-mail address removed) (Aahz) wrote in

Simplest:

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

Fastest:

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

...

if testvar in options:


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

Scott
 
F

F. Petitjean

testvar = bob
I suppose you mean : testvar = "bob"
# Trial A: This does not work
if testvar == ("fred" or "bob):
statements
How about this ?
if testvar in ("fred", 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?

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


Regards
 
P

Paul Rubin

Scott F said:
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?

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

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

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top