Conditionals and Booleans

Part of: Programming Lab: Python for CSP

Making Decisions. A conditional lets a program choose between paths. It tests a Boolean , a value that is either True or False, and runs different code depending on the result. The test age = 18 produces a Boolean. If it is True, the indented block under if runs. If it is False, the block under else runs. Only one branch runs, never both. Comparison Operators. You build Booleans with comparisons: == (equal), != (not equal), <, , <=, =. Notice == with two equals signs tests equality, while a single = assigns. Mixing them up is a classic beginner bug. More Than Two Choices. Use elif (short for else if) to check several conditions in order. Python tries each test top to bottom and runs the first block whose test is True. Order matters here. A score of 95 matches the first test and stops, so it never reaches the elif. What usually goes wrong. What you see What caused it How to fix it --- --- --- SyntaxError: invalid syntax pointing at the if line A single equals sign was used where a comparison belonged A test needs two equals signs. One equals sign only stores a value IndentationError: expected an indented block The line under if or else was left flush against the margin Indent every

Challenge: Even or Odd