# ============================================================ # AP CSP — FOR AND WHILE LOOP REFERENCE BUILDER # ============================================================ # # NAME: # CLASS: # # DIRECTIONS: # Write working Python code under each set of comments. # Run and test each example before moving to the next. # Keep every completed example in this ONE sandbox. # # Each section should demonstrate a different way to # control repetition. # # IMPORTANT: # Use print() to make the behavior of each loop visible. # Add comments explaining any surprising output. # # ============================================================ # PART 1 — FOR LOOPS # ============================================================ # 1. BASIC COUNTING # Create a for loop that prints the numbers 0 through 4. # Use range(). # 2. START AND STOP # Create a for loop that prints the numbers 3 through 8. # Specify both the starting and stopping values. # Remember that the stopping value is excluded. # 3. COUNT BY TWOS # Create a for loop that prints 2, 4, 6, 8, and 10. # Use the step argument in range(). # 4. COUNT BACKWARD # Create a for loop that prints 10 down to 1. # Use a negative step. # 5. SKIP NUMBERS # Create a for loop that prints every third number # starting at 0 and ending at 15. # 6. REPEAT AN ACTION # Create a for loop that prints "Loading..." exactly # five times. # Explain how the loop controls the number of repetitions. # 7. ACCUMULATOR # Create a variable named total starting at 0. # Use a for loop to add the numbers 1 through 5. # Print the final total AFTER the loop. # 8. COUNTER WITH A CONDITION # Use a for loop to examine the numbers 1 through 10. # Count how many are greater than 6. # Print the count after the loop. # 9. SELECTION INSIDE ITERATION # Use a for loop to examine numbers 1 through 10. # Print "Even" for even numbers and "Odd" for odd numbers. # Use the remainder operator %. # 10. TRAVERSE A LIST # Create a list containing at least five game items. # Use a for loop to print each item on a separate line. # 11. SEARCH A LIST # Create a list containing "map" and several other items. # Use a for loop and an if statement to find "map". # Print "Map found!" when the item is encountered. # 12. TRAVERSE BY INDEX # Create a list containing four names. # Use range() and len() to visit the list by index. # Print both the index and the element at that index. # Compare this with directly traversing the elements. # ============================================================ # PART 2 — WHILE LOOPS # ============================================================ # 13. BASIC WHILE LOOP # Create a counter starting at 1. # Use a while loop to print 1 through 5. # Increase the counter inside the loop. # 14. COUNT BACKWARD WITH WHILE # Start a counter at 10. # Use a while loop to print 10 down to 1. # Decrease the counter each time. # 15. CONTROL WITH A COMPARISON # Start a variable named temperature at 65. # Use a while loop that runs while temperature < 70. # Print the temperature and increase it by 1 each time. # 16. CONTROL WITH USER INPUT # Ask the user to enter a password. # Keep asking while the password is not "python". # Print "Access granted" after the loop finishes. # 17. SENTINEL-CONTROLLED LOOP # Repeatedly ask the user to enter a number. # Stop when the user enters -1. # Print each number except the sentinel value. # A SENTINEL is a special value that signals "stop". # 18. BOOLEAN FLAG # Create a Boolean variable named running set to True. # Use running as the while loop condition. # Ask the user whether to continue. # Change running to False when they enter "no". # Make sure the loop can terminate. # 19. COMPOUND CONDITIONS # Create a counter starting at 0. # Use a while loop that continues while the counter # is less than 10 AND is not equal to 5. # Print the counter and increase it each time. # Explain why the loop stops. # 20. WHILE WITH AN ACCUMULATOR # Create a total starting at 0 and a counter at 1. # Use a while loop to add 1 through 10. # Print the final total. # 21. INPUT VALIDATION # Ask the user to enter a number from 1 to 5. # Keep asking while the number is outside that range. # Print "Valid choice" once the input is acceptable. # Assume the user enters a valid integer each time. # 22. SIMULATE A COUNTDOWN # Create a countdown starting at 5. # Use a while loop to print each number down to 1. # Print "Launch!" after the loop. # ============================================================ # PART 3 — CONTROLLING LOOP EXECUTION # ============================================================ # 23. BREAK # Use a for loop to examine numbers 1 through 10. # Stop the loop immediately when the number equals 6. # Print the numbers encountered before stopping. # BREAK exits the nearest enclosing loop. # 24. CONTINUE # Use a for loop to examine numbers 1 through 10. # Skip printing the number 5. # Print every other number. # CONTINUE skips the rest of the current iteration. # 25. BREAK INSIDE A WHILE LOOP # Create a while True loop. # Ask the user to type a command. # Exit the loop with break when they type "quit". # Otherwise print the command. # Make sure there is a reachable way to exit. # 26. MULTIPLE EXIT CONDITIONS # Create a while loop that simulates a player exploring. # Begin with 5 energy points. # Each iteration uses 1 energy point. # Ask the player if they want to quit. # Stop when energy reaches 0 OR the player quits. # Print why the loop ended. # 27. NESTED LOOPS # Create an outer for loop that runs 3 times. # Inside it, create another for loop that runs 2 times. # Print both loop counters. # Explain why the inner loop runs 6 times total. # ============================================================ # PART 4 — AP CSP CREATE TASK CONNECTIONS # ============================================================ # 28. LIST + ITERATION + SELECTION # Create a list of at least five test scores. # Use a loop to examine every score. # Use an if statement to count scores of 70 or higher. # Print the final count. # # Identify: # - The list # - The iteration # - The selection # - The counter # 29. STUDENT-DEVELOPED PROCEDURE # Define a function with a parameter that receives a list. # Inside the function, use a loop and an if statement. # Return a calculated result. # Call the function and print the returned result. # # Your function should contain: # - Sequencing # - Selection # - Iteration # - At least one parameter # 30. FINAL CHALLENGE — CHOOSE THE RIGHT LOOP # Build a simple game menu. # Store at least three game items in a list. # Repeatedly ask the player to choose an action. # Let the player display the items or quit. # Use a for loop to display the list. # Use a while loop to keep the menu running. # Use selection to respond to the player's choice. # # When finished, explain: # Why was for useful for the list? # Why was while useful for the menu? # What condition causes the while loop to end? # ============================================================ # FINAL REFLECTION # ============================================================ # # 1. When is a for loop usually the better choice? # # 2. When is a while loop usually the better choice? # # 3. What is the difference between break and continue? # # 4. What can cause an infinite loop? # # 5. How can iteration and selection work together? # # 6. How could a list and a loop help manage complexity # in your AP CSP Create performance task? # # ============================================================