Reputation: 115
Here is my aim in an example. If you could help me complete it that would be great!
exampleNumbers = [One,Uno,Two,Dos]
randomNumber = random.choice(exampleNumbers)
From here on I want it to then change randomNumber
to 1 if the random selection of exampleNumbers
is One or Uno or change randomNumber
to 2 if the random selection of exampleNumbers
is Two or Dos.
I think I can do it using an if statement, however I am unsure on how to use an if statement with multiple values.
So basically if the random selection is Two for example, I want it to then make randomNumber
= 2.
Sorry if I haven't explained this very well, I'm slowly getting there but my knowledge and terminology is still at an amateur state. I'll happily tick and vote up any great answers!
Thanks in advance! :)
Upvotes: 0
Views: 1100
Reputation: 73
You could also define options:
options = { One : thisIsOne,
Uno : thisIsOne,
Two : thisIsTwo,
Dos : thisIsTwo,
}
def thisIsOne()
randomNumber = 1
def thisIsTwo()
randomNumber = 2
You can then simply call options[randomNumber]()
I'm a little rusty with my python but I'm pretty sure that works (but so does the previous answer by rodrigo! matter of preference and reusability)
Upvotes: 0
Reputation: 428
you can use and
and or
to compound statements. and for multiple statements you can also use parenthesis (ie: (True and False) or (False or True)
). There does exist an order of operations which is basically left to right. So for you question you can do
if(num == "two" or num == "dos"):
//do things
elif (num == "one" or num == "uno"):
//do other things
Upvotes: 0
Reputation: 98338
You can use the operator in
:
if randomNumber in (One,Uno):
randomNumber = 1
else:
randomNumber = 2
Or the classic or
boolean operator:
if randomNumber == One or randomNumber == Uno:
randomNumber = 1
else:
randomNumber = 2
The in
is great to check for a lot of values.
The or
, with the other boolean operators and
and not
can be used to build arbitrarily complex logical expressions.
Upvotes: 2