Heavy logic, and how to avoid it improving code

I have this (working) piece of python code, whose purpose is to return a string (actually a Bootstrap button label) depending on a 7-condition set:

....
    if cond1:

        if cond2:

            if cond3:
                ...
                return string10
            else:
                ...
                return string11
        else:
            if cond4:

                if cond5:
                    ...
                    return string9
                else:
                    if cond6:
                        ...
                        return string7
                    else:
                        ...
                        return string8
            else:
                if cond7:
                    ...
                    return string6
                else:
                    if cond6:
                       ...
                       return string5
                    else:
                        if cond3:
                            ...
                            return string3
                        else:
                            ...
                            return string4
    else:
        if cond2:
            ...
            return string1                                                                                                                                    
        else:
            ...
            return string2

I know this is horrible, and also difficult to maintain. My question is, how to improve it?
One thing I tried was to ‘linearize’ and get rid of the tree-like path, and having instead:

CompositeCond1=(mess made with and's and or's on cond1--cond7)
...
CompositeCond11=(mess made with and's and or's on cond1--cond7)

if CompositeCond1:
   ...
   return string1
(more of this...)
if CompositeCond11:
   ...
   return string11

but it seems little help, and I am not sure I like it more.

Any other ideas? Thank you!

One small improvement is to use elif.

if cond1:
    ….
elif cond2:
    …
else:
    …
1 Like

depending on your specifics, a match statement could also be an improvement. eg:

match (
  condition1,
  condition2,
):
  case (True, False): return string1
  case (True,True): return string2
  case (False, _): return string3

And back in Olden Times, before we had the match statement, a dict would often be used to build a “logic table” to do that sort of matching.

partial_result = {
   (True, False, "whatever"): "String1",
   (False, True, "yo mama"): "String2",
... and so on ...
}

result = partial_result.get((var1 == "x", var2 == "y", var3), "default-string")

I went the “match” way, the “if…elif” alternative being equally convincing.

I gained a lot by felling my silly decision-tree:

  1. without indents all my line are shorter and of about equal legth (not so unimportant, IMHO) and
  2. it is easier now to permute the order of my alternatives, for better understanding; before one needed a lot of attention in pruning and grafting pieces of the structure.

To all who answered: thank for your time and interest!