Flag

Tags
Summary

Use a variable as a flag. Set the flag if any of a number of things happens. After, check the flag to see if any of those things happened.

Situation

You want one code chunk to tell another code chunk what happened. For example, validation code tells processing code whether there were validation errors.

Needs

A variable you can use as a flag.

Provides

A variable you can test to see of one of a number of things happened.

Action

Initialize a variable. E.g.:

  • is_life_ok = True

If something happens you want to remember, change the flag.

  • if doggo_count == 0:
  •     print("Get some doggos!")
  •     is_life_ok = False
  • if laugh_count < 10:
  •     print("Get more funny friends!")
  •     is_life_ok = False
  • ... More tests as required.

Check the flag after all the tests:

  • if not is_life_ok:
  •     print("Sorry your life sucks.\n")

You can also check the flag after the first test, to skip tests after one failure.

  • is_life_ok = True
  • if doggo_count == 0:
  •     print("Get some doggos!")
  •     is_life_ok = False
  • if is_life_ok:
  •     if laugh_count < 10:
  •         print("Get more funny friends!")
  •         is_life_ok = False
  •  
  • ... More tests as required.
  •  
  • if not is_life_ok:
  •     print("Sorry your life sucks.\n")
Where referenced