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() ?

Member Avatar for leegeorg07

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 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()

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()

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

I'll select woooee's solution
Thanks everybody. Sometimes we lack a goto in python...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.