The Password Checker was the first program you wrote that made a decision โ read a password, weigh it, answer accordingly. That fork is a conditional โ how programs stop being recordings and respond.
One question, two paths
password = input("Choose a password: ")
if len(password) >= 8:
print("Long enough.")
else:
print("Too short โ aim for 8 or more.")if asks a yes-or-no question: yes runs the first block, no runs the
else block. Exactly one of the two paths ever happens.
More than two paths
elif โ โelse ifโ โ chains more questions, checked top to bottom:
if len(password) >= 12:
print("Strong.")
elif len(password) >= 8:
print("Acceptable.")
else:
print("Too short.")Order matters: Python takes the first yes and skips the rest.
The operators that ask the questions
Comparisons produce the yes or no: ==, !=, <, >, <=, >=.
Booleans combine answers โ and (both), or (either), not (flip).
The classic trap: = assigns, == compares โ see Spot the Bug.
Questions inside questions
Checks can nest โ ask a second question only if the first passes:
flowchart TD A{8 or more characters?} -- no --> R[Reject] A -- yes --> B{Contains a digit?} B -- no --> R B -- yes --> OK[Accept]
Nesting is easy to overgrow โ three deep and squinting, flatten with
and. Then Conditionals Practice makes the moves automatic.
Curriculum connection
C1.5
identify and explain situations in which conditional and repeating structures are required
Link to original
C2.3
write programs that include single and nested conditional statements
Link to original
C2.5
write programs that include the use of Boolean operators, comparison operators, text operators, and arithmetic operators
Link to original