Decision Logic SlidesSYSTEM.INIT() Decision Logic Mastering Control Flow in Python >>> 11TH GRADE CS What is Control Flow? Programs usually run from top to bottom, one line at a time. "Control flow is the order in which individual statements, instructions or function calls are executed or evaluated." In Python, we use conditionals to make choices. # The Linear Path print("Waking up...") print("Making coffee...") print("Coding...") # The Branched Path if is_tired: print("Nap time!") Python Conditionals SYNTAX if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") else: print("Keep studying!") The Colon (:) Every conditional header must end with a colon. Indentation The code inside the block MUST be indented (usually 4 spaces). Elif & Else Use elif for multiple checks and else for everything else. Comparison Operators The tools for making decisions == Equal To Checks if values match != Not Equal Checks if values differ > < Greater/Less Standard numeric comparison and Logical AND Both must be true or Logical OR At least one must be true not Logical NOT Flips the boolean value The Indentation Trap Crucial Python Rule In Python, indentation isn't just for looks. It defines the scope of your code. Consistent indentation is required (4 spaces is the standard). Mixing tabs and spaces causes errors. The indentation level tells Python which code belongs to the if. # Correct if hungry: eat_snack() print("I'm done.") # Common Mistake if hungry: eat_snack() # IndentationError! LIVE CODE CHALLENGE The Secret Password Write a script that asks a user for a password. If it matches "python123", print "Access Granted". Otherwise, print "Access Denied". Step 1 Get input from user Step 2 Use if / else Step 3 Print the result
Decision Logic Teacher GuideTeacher Facilitation Guide Lesson: Decision Logic | Python Pathways Sequence 11th Grade CS Lesson Objective Students will be able to construct and trace the execution of single and multi-branch conditional statements in Python, applying correct syntax and logical operators to solve branching problems. Standards Alignment CSTA 3A-AP-15: Justify the design of an algorithm. CSTA 3A-AP-12: Design and implement algorithms that use control structures. Instructional Roadmap 05m Activation: The "If-Then" Human Race Have students stand up. Give instructions like: "If you are wearing blue, take a step forward. Else if you have glasses, clap once. Else, sit down." Key Question: "Why did some people sit down while others stayed standing? How did the order of my commands change who moved?" 15m Direct Instruction: Syntax & Rules Use the Decision Logic Slides to introduce syntax. Focus heavily on Indentation and the Colon—these are the top two errors for beginners. if condition: elif alternative: else: 20m Guided Practice: Trace & Correct Distribute the Logic Lab Worksheet. Work through the first tracing problem together as a class on the board. Emphasize that only ONE block in an if-elif-else chain will ever run. Highlight the difference between = (assignment) and == (equality). 10m Synthesis: Independent Coding Students complete the final challenge on the worksheet. Challenge early finishers to "nest" an if-statement (an if inside an if). Common Misconceptions Missing Colons: Students often forget the : at the end of the line. Conditional Overlap: Thinking multiple blocks will run if multiple conditions are true (they won't in an elif chain). Assignment vs Comparison: Using if x = 5: instead of if x == 5:. Pro-Tip: Mental Models Tell students to think of a conditional chain like a series of gates. As soon as you find one that's open (True), you walk through it and skip all the other gates in that group. >>> "Control flow is the skeleton of logic."
Logic Lab WorksheetStudent.Worksheet Logic Lab: Decision Making Module 02: Control Flow in Python NAME: DATE: 1 The Indentation Audit Identify the two syntax errors in the following code block and describe how to fix them. 1: age = 17 2: if age >= 18 3: print("You can vote!") 4: else: 5: print("Wait a few years.") ERROR #1 ERROR #2 2 Trace the Path Predict what will be printed for each value of x. # Grading Logic if x > 80: print("Exceeds") elif x >= 60: print("Meets") else: print("Developing") x = 80 x = 81 x = 60 x = 59 3 Code Sketching The Weather App Bot Write a Python script that takes a variable temp (integer). • If temp is 85 or above: print "Too hot!" • If temp is between 60 and 84 (inclusive): print "Perfect day." • Otherwise: print "Bring a jacket." 12345678910 Challenge Question: What would happen if we used three separate if statements instead of an if-elif-else chain? Would the result always be the same?
Logic Lab Answer KeyAnswer Key Logic Lab: Solved 1. The Indentation Audit Error 1 Line 2: Missing colon after if age >= 18. Fix: add ":" at the end of the line. Error 2 Line 3: Incorrect indentation. The print statement must be indented. Fix: Add 4 spaces before print. 2. Trace the Path x = 80 Meets x = 81 Exceeds x = 60 Meets x = 59 Developing 3. Code Sketching Sample temp = 72 # or any input if temp >= 85: print("Too hot!") elif temp >= 60: print("Perfect day.") else: print("Bring a jacket.") Challenge Question Answer No, the result would not always be the same. With separate if statements, multiple blocks could execute. For example, if temp = 90, a script with separate ifs might print "Too hot!" AND "Perfect day." because 90 is greater than both 85 and 60. The elif ensures only the first true condition runs.