Programming from zero

Decide if a deploy is allowed

mediumIf and else

Problem statement

A deploy pipeline has three rules, checked in this order:

  1. If tests_passed is False, print Deploy blocked: tests failed.
  2. Otherwise, if env is "production" and approved is False, print Deploy blocked: production needs approval.
  3. 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.

Python
env = "production"
tests_passed = True
approved = 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 freeze variable is True.

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.