{ "cells": [ { "cell_type": "markdown", "id": "1e3007e1", "metadata": {}, "source": [ "# Lesson 05 activity: Loops and conditionals challenge\n", "\n", "In this activity, you'll solve four problems that build on what you've learned about conditional statements and loops in Lesson 05. These problems will require you to **think creatively**, **combine concepts**, and **explore** different approaches to solving problems with loops and conditionals.\n", "\n", "## Instructions:\n", "- Each problem has a clear objective\n", "- You may need to research, experiment, or combine multiple concepts\n", "- Test your solutions in the code cells provided\n", "- There are multiple ways to solve each problem - be creative!" ] }, { "cell_type": "markdown", "id": "c0839fd4", "metadata": {}, "source": [ "---\n", "## Problem 1: The password checker\n", "\n", "**Objective:** Check whether a list of passwords meets security criteria using loops and conditionals.\n", "\n", "**Given:**\n", "```python\n", "passwords = ['Pass123', 'password123', 'PASSWORD123', 'Password', 'Password123']\n", "```\n", "\n", "**Your Task:**\n", "Write code that loops through each password and checks if it meets these rules:\n", "- At least 8 characters long\n", "- Contains at least one uppercase letter\n", "- Contains at least one lowercase letter\n", "- Contains at least one digit (0-9)\n", "\n", "For each password, print a message for each rule that is not met. If all rules are met, print `'Password is valid!'`.\n", "\n", "**Expected output (first two passwords):**\n", "```\n", "Checking: Pass123\n", " Password must be at least 8 characters long\n", "\n", "Checking: password123\n", " Password must contain at least one uppercase letter\n", "```\n", "\n", "**Hints:**\n", "- Use a `for` loop to iterate over each character in the password\n", "- Use boolean flags like `has_upper`, `has_lower`, `has_digit` to track which criteria are met\n", "- Use string methods like `.isupper()`, `.islower()`, `.isdigit()`\n", "- Reset your flags at the start of each password check" ] }, { "cell_type": "code", "execution_count": null, "id": "f0896a9f", "metadata": {}, "outputs": [], "source": [ "# Problem 1: Your solution here\n", "\n", "passwords = ['Pass123', 'password123', 'PASSWORD123', 'Password', 'Password123']\n", "\n", "for password in passwords:\n", " print(f'\\nChecking: {password}')\n", "\n", " # Reset flags for each password\n", " has_upper = False\n", " has_lower = False\n", " has_digit = False\n", "\n", " # Write your code here: loop through each character and set the flags\n", "\n", " # Write your code here: check each criterion and print messages\n" ] }, { "cell_type": "markdown", "id": "7e5027de", "metadata": {}, "source": [ "---\n", "## Problem 2: The number pattern printer\n", "\n", "**Objective:** Create a triangle number pattern using nested loops.\n", "\n", "**Your Task:**\n", "Write code that prints this triangle pattern for `n = 5` rows:\n", "```\n", "1 \n", "1 2 \n", "1 2 3 \n", "1 2 3 4 \n", "1 2 3 4 5 \n", "```\n", "\n", "Then change `n` to `3` and `4` to verify the pattern scales correctly.\n", "\n", "**Hints:**\n", "- Use nested loops: an outer loop for each row, and an inner loop for the numbers in each row\n", "- The outer loop runs from `1` to `n` (inclusive)\n", "- The inner loop runs from `1` to the current row number (inclusive)\n", "- Use `print(col, end=' ')` to print numbers on the same line\n", "- Use `print()` with no arguments to move to the next line after each row" ] }, { "cell_type": "code", "execution_count": null, "id": "fa826b52", "metadata": {}, "outputs": [], "source": [ "# Problem 2: Your solution here\n", "\n", "n = 5\n", "\n", "print(f'Triangle with {n} rows:')\n", "\n", "# Write your nested loops here\n" ] }, { "cell_type": "markdown", "id": "34e25af5", "metadata": {}, "source": [ "---\n", "## Problem 3: The prime number finder\n", "\n", "**Objective:** Find all prime numbers in a given range using nested loops and conditionals.\n", "\n", "**Background:**\n", "A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers.\n", "\n", "**Your Task:**\n", "Write code that finds all prime numbers between `start = 1` and `end = 20`:\n", "- Use a loop to check each number in the range\n", "- For each number, use an inner loop to test whether it has any divisors\n", "- Collect all prime numbers in a list called `primes`\n", "- Print the list and the total count\n", "\n", "**Expected output:**\n", "```\n", "Primes from 1 to 20:\n", "[2, 3, 5, 7, 11, 13, 17, 19]\n", "Found 8 prime numbers\n", "```\n", "\n", "**Hints:**\n", "- A number is prime if it is not divisible by any number from `2` to its square root\n", "- Use the modulo operator `%` to check divisibility: if `num % divisor == 0`, it is not prime\n", "- Use a boolean flag `is_prime` and set it to `False` when a divisor is found\n", "- Use `break` to stop checking once you know a number is not prime\n", "- Use `int(num ** 0.5) + 1` as the upper bound of the inner loop for efficiency" ] }, { "cell_type": "code", "execution_count": null, "id": "1f8e4c22", "metadata": {}, "outputs": [], "source": [ "# Problem 3: Your solution here\n", "\n", "start = 1\n", "end = 20\n", "\n", "primes = []\n", "\n", "# Write your nested loops here\n", "\n", "print(f'Primes from {start} to {end}:')\n", "print(primes)\n", "print(f'Found {len(primes)} prime numbers')\n" ] }, { "cell_type": "markdown", "id": "947a421a", "metadata": {}, "source": [ "---\n", "## Problem 4: Fixing buggy loops and conditionals\n", "\n", "**Objective:** Debug and fix code snippets that contain common loop and conditional errors.\n", "\n", "**Your Task:**\n", "Below are three code snippets that contain bugs. For each one:\n", "1. Identify the error\n", "2. Fix the code\n", "3. Test that it works correctly\n", "4. Add a comment explaining what was wrong\n", "\n", "Run each fixed snippet to verify it works!" ] }, { "cell_type": "markdown", "id": "513ba425", "metadata": {}, "source": [ "### Bug 1\n", "\n", "This code is supposed to print numbers from 1 to 5.\n", "\n", "**Expected output:** \n", "```\n", "1\n", "2\n", "3\n", "4\n", "5\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "4656b1c9", "metadata": {}, "outputs": [], "source": [ "# Bug 1: Fix the code below\n", "\n", "counter = 1\n", "\n", "while counter <= 5:\n", " print(counter)" ] }, { "cell_type": "markdown", "id": "37c5263e", "metadata": {}, "source": [ "### Bug 2\n", "\n", "This code should print even numbers from 2 to 10.\n", "\n", "**Expected output:**\n", "```\n", "2\n", "4\n", "6\n", "8\n", "10\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "a80eac78", "metadata": {}, "outputs": [], "source": [ "# Bug 2: Fix the code below\n", "\n", "for i in range(2, 10):\n", " print(i)" ] }, { "cell_type": "markdown", "id": "f385db24", "metadata": {}, "source": [ "### Bug 3\n", "\n", "This code should classify grades scores according to:\n", "- A: 90-100\n", "- B: 80-89\n", "- C: 70-79\n", "- D: 60-69\n", "- F: below 60\n", "\n", "For `score = 85`:\n", "\n", "**Expected output:** `Score: 85, Grade: B`\n", "\n", "**Current (wrong) output:** `Score: 85, Grade: F`" ] }, { "cell_type": "code", "execution_count": null, "id": "dc024ddc", "metadata": {}, "outputs": [], "source": [ "# Bug 3: Fix the code below\n", "\n", "score = 85\n", "\n", "if score >= 90:\n", " grade = 'A'\n", "if score >= 80:\n", " grade = 'B'\n", "if score >= 70:\n", " grade = 'C'\n", "if score >= 60:\n", " grade = 'D'\n", "else:\n", " grade = 'F'\n", "\n", "print(f'Score: {score}, Grade: {grade}')\n" ] }, { "cell_type": "markdown", "id": "2194a85b", "metadata": {}, "source": [ "---\n", "## __Reflection questions__\n", "\n", "After completing the challenges, answer these questions:\n", "\n", "1. Which problem required the most nested loops? How did you keep track of the logic?\n", "2. How did you decide when to use a `for` loop vs a `while` loop?\n", "3. What was the most challenging aspect of combining loops and conditionals?\n", "4. Did you use `break` or `continue` statements? If so, where and why?\n", "5. What debugging strategies did you use when your loops weren't working as expected?" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }