Decide if a deploy is allowed
Problem statement
A deploy pipeline has three rules, checked in this order:
- If
tests_passedisFalse, printDeploy blocked: tests failed. - Otherwise, if
envis"production"andapprovedisFalse, printDeploy blocked: production needs approval. - Otherwise print
Deploy allowed.
Examples
Example 1
Input: env = "production"
tests_passed = True
approved = False
Output: Deploy blocked: production needs approval
Hints
Approach
Optimal
Booleans (True and False) can be used directly as conditions, so if not tests_passed: reads almost like English. The keyword not flips a boolean, and and combines two conditions so the result is true only when both are. Rule order matters: failed tests block every deploy, so that check comes first. The approval rule only applies to production, so it combines two conditions with and. If neither blocking rule matches, the else branch allows the deploy.
env = "production"tests_passed = Trueapproved = False if not tests_passed: print("Deploy blocked: tests failed")elif env == "production" and not approved: print("Deploy blocked: production needs approval")else: print("Deploy allowed")Follow-up questions
- Add a rule: deploys to any environment are blocked when a
freezevariable isTrue.
Frequently asked questions
It works, but if not tests_passed: is the usual Python style and is shorter. Comparing booleans to True or False is a common beginner habit worth dropping.
a and b needs both to be true. a or b needs at least one. For example, env == "production" or env == "staging" matches either environment.