breakshooter
breakshooter

Reputation: 69

Given two "if" statements, execute some code if none of them is executed

hey i don't know how to programm this constellation :

string = " "
if "abc" in string:
    print ("abc is in string")
if "def" in string:
    print ("def is in string")
else:
    print ("abc and def are not contained in string")

It should go to "else" only if the 2 conditions aren't true. But if both substrings are contained in string; it should print both.

Upvotes: 6

Views: 82

Answers (5)

Jacques Gaudin
Jacques Gaudin

Reputation: 17008

You could use a dictionary as an equivalent of a switch case statement (although it slightly changes the output):

msg = {
    (True, True): "abc and def in string",
    (True, False): "abc in string",
    (False, True): "def in string",
    (False, False): "neither abc nor def in string"
}[("abc" in string, "def" in string)]

print(msg)

Upvotes: 0

Netwave
Netwave

Reputation: 42796

How about looping through them?, this is completly generic and will work for any number of strings to check you may need.

string = " "
strs = ("abc", "def")
if any(s in string for s in strs):
    for s in strs:
        if s in string:
            print ("{} is in string".format(s))
else:
    print (" and ".join(strs) + " are not contained in string")

Here you have a live example

Upvotes: 1

VPfB
VPfB

Reputation: 17387

I'd like to show another approach. The advantage is that it divides the code in two logical steps. However in simple cases like this example question it is probably not worth the extra effort.

Those two steps are: 1. obtain all partial results; 2. process them all

DEFAULT = ["abc and def are not contained in string"]
string = "..."

msglist = []
if "abc" in string:
    msglist.append("abc is in string")
if "def" in string:
    msglist.append("def is in string")
# more tests could be added here

msglist = msglist or DEFAULT
for msg in msglist:
    print(msg)
    # more processing could be added here

Upvotes: 2

brandizzi
brandizzi

Reputation: 27100

Another option is to use a variable that is true only if a condition is satisfied before. This variable (let us call it found) will be false by default:

found = False

However, in each of the if statements, we set it to True:

if "abc" in string:
    print ("abc is in string")
    found = True

if "def" in string:
    print ("def is in string")
    found = True

Now we only have to check the variable. If any of the conditions where met, it will be true:

if not found:
    print ("abc and def are not contained in string")

That is only one of the options to solve this, but I have seen this pattern used many times. Of course, you can choose other approach if you feel it would be better.

Upvotes: 2

LoicM
LoicM

Reputation: 2213

You could simply define a boolean for each of your condition It keeps the code simple

abc = "abc" in string
def_ = "def" in string
if abc : 
    print("abc in string")
if def_ : 
    print("def in string")
if not (abc or def_) : 
    print("neither abc nor def are in this string")

Upvotes: 2

Related Questions