0

What do you think would be the best way to write this structure

if testA:
  actionA()
  if testB:
    actionB()
    if textC:
      actionC()
    else:
      actionDefault()
  else:
    actionDefault()
else:
  actionDefault()

if I want to write only one call to actionDefault() ?

4
Contributors
5
Replies
7
Views
8 Years
Discussion Span
Last Post by Gribouillis
0

if testA:
actionA()
if testB:
actionB()
if textC:
actionC()
else:
actionDefault()

this will bypass all the others as if any of the test things arent right it will skip it and go to action default

1

This will only execute actionDefault once.

do_action = 0
if testA:
  actionA()
  if testB:
    actionB()
    if textC:
      actionC()
    else:
      do_action=1
  else:
    do_action=1
else:
  do_action=1
if do_action:
  actionDefault()
0

This would do the trick, but would not be very readable:

if testA:
    actionA()
    if testB:
        actionB()
        if testC:
            actionC()

if not(testA and testB and testC):
   actionDefault()
0

if testA:
actionA()
if testB:
actionB()
if textC:
actionC()
else:
actionDefault()

this will bypass all the others as if any of the test things arent right it will skip it and go to action default

This will not work for:
testA = True
testB = False
testC = False

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.