{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "77447a22",
   "metadata": {},
   "source": [
    "# Lesson 04: Scavenger Hunt Activity\n",
    "\n",
    "In this activity, you'll solve four problems that build on what you've learned in Lesson 04. However, these problems will require you to **research**, **explore**, and **discover** Python methods and techniques not directly covered in the demonstration notebooks.\n",
    "\n",
    "## Instructions:\n",
    "- Each problem has a clear objective\n",
    "- You may need to use Google, Python documentation, or experimentation\n",
    "- Test your solutions in the code cells provided\n",
    "- There are multiple ways to solve each problem - be creative!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68cc8b66",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 1: The Word Splitter\n",
    "\n",
    "**Objective:** Split a sentence into a list of individual words.\n",
    "\n",
    "**Given:**\n",
    "```python\n",
    "text = \"Python is awesome!\"\n",
    "```\n",
    "\n",
    "**Your Task:**\n",
    "- Research string methods that can break a sentence into parts\n",
    "- Split the string `text` into a list of words and store it in a variable called `word_list`\n",
    "- Print both the original sentence and the list of words\n",
    "- Print the number of words in the sentence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40496d62",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Problem 1: Your solution here\n",
    "text = \"Python is awesome!\"\n",
    "\n",
    "# Write your code below\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15f70b88",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 2: The Dictionary Saver\n",
    "\n",
    "**Objective:** Save a Python dictionary to a file and then load it back.\n",
    "\n",
    "**Given:**\n",
    "```python\n",
    "student_data = {\n",
    "    'name': 'Alice',\n",
    "    'age': 22,\n",
    "    'courses': ['Python', 'Data Science', 'Machine Learning'],\n",
    "    'grade': 'A'\n",
    "}\n",
    "```\n",
    "\n",
    "**Your Task:**\n",
    "1. Save the `student_data` dictionary to a file called\n",
    "2. Load the data back from the file into a new ictionary called `loaded_data`\n",
    "3. Print both the original and loaded dictionaries to verify they match\n",
    "4. Use error handling to manage any potential issues\n",
    "\n",
    "**Hint:** JSON (JavaScript Object Notation) is a popular format for storing structured data. thon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93e02c6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Problem 2: Your solution here\n",
    "\n",
    "# Given dictionary\n",
    "student_data = {\n",
    "    'name': 'Alice',\n",
    "    'age': 22,\n",
    "    'courses': ['Python', 'Data Science', 'Machine Learning'],\n",
    "    'grade': 'A'\n",
    "}\n",
    "\n",
    "# Write your code below to save and load the "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e45715de",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 3: The Safe List Accessor\n",
    "\n",
    "**Objective:** Write a function that safely retrieves an element from a list without causing an `IndexError`.\n",
    "\n",
    "**Given:**\n",
    "```python\n",
    "my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']\n",
    "```\n",
    "\n",
    "**Your Task:**\n",
    "1. Create a function called `safe_get(my_list, index, default=None)` that:\n",
    "   - Takes a list, an index, and an optional default value\n",
    "   - Returns the element at the specified index if it exists\n",
    "   - Returns the default value (or `None`) if the index is out of range\n",
    "   - Does NOT raise an `IndexError`\n",
    "2. Test your function with both valid and invalid indices\n",
    "\n",
    "**Test Cases:**\n",
    "- `safe_get(my_list, 2)` → should return `'cherry'`\n",
    "- `safe_get(my_list, 10)` → should return `None`\n",
    "- `safe_get(my_list, -1)` → should return `'elderberry'`\n",
    "- `safe_get(my_list, 100, 'Not found')` → should return `'Not found'`\n",
    "\n",
    "**Hint:** Remember the error handling techniques from the demo notebook!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17edfb8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Problem 3: Your solution here\n",
    "\n",
    "def safe_get(my_list, index, default=None):\n",
    "    \"\"\"\n",
    "    Safely retrieves an element from a list without raising IndexError.\n",
    "    \n",
    "    Args:\n",
    "        my_list (list): The list to access\n",
    "        index (int): The index to retrieve\n",
    "        default: The value to return if index is out of range (default: None)\n",
    "    \n",
    "    Returns:\n",
    "        The element at the index, or the default value if out of range\n",
    "    \"\"\"\n",
    "    # Write your code here (remove the pass statement)\n",
    "    pass\n",
    "\n",
    "# Test cases\n",
    "my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']\n",
    "\n",
    "print(\"Test 1:\", safe_get(my_list, 2))\n",
    "print(\"Test 2:\", safe_get(my_list, 10))\n",
    "print(\"Test 3:\", safe_get(my_list, -1))\n",
    "print(\"Test 4:\", safe_get(my_list, 100, 'Not found'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b030bdf",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 4: Fixing Bugs\n",
    "\n",
    "**Objective:** Debug and fix code snippets that contain common Python errors.\n",
    "\n",
    "**Your Task:**\n",
    "Below are several 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": "e438028b",
   "metadata": {},
   "source": [
    "### Bug 1: The Missing Quotes\n",
    "```python\n",
    "print(Hello, World!)\n",
    "```\n",
    "**Expected Output:** `Hello, World!`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89d1a0c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Bug 1: Fix the code below\n",
    "# Error: \n",
    "\n",
    "print(Hello, World!)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29a63bcc",
   "metadata": {},
   "source": [
    "### Bug 2: The Indentation Problem\n",
    "```python\n",
    "def greet(name):\n",
    "print(f\"Hello, {name}!\")\n",
    "return \"Greeting complete\"\n",
    "```\n",
    "**Expected Behavior:** Function should print a greeting and return the message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "219901ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Bug 2: Fix the code below\n",
    "# Error: \n",
    "\n",
    "def greet(name):\n",
    "print(f\"Hello, {name}!\")\n",
    "return \"Greeting complete\"\n",
    "\n",
    "# Test it\n",
    "result = greet(\"Student\")\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28bbe6fa",
   "metadata": {},
   "source": [
    "### Bug 3: The Division Disaster\n",
    "```python\n",
    "def calculate_average(numbers):\n",
    "    total = sum(numbers)\n",
    "    average = total / len(numbers)\n",
    "    return average\n",
    "\n",
    "scores = []\n",
    "result = calculate_average(scores)\n",
    "print(f\"Average: {result}\")\n",
    "```\n",
    "**Expected Behavior:** Should handle empty lists gracefully without crashing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6a0e1d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Bug 3: Fix the code below\n",
    "# Error: \n",
    "\n",
    "def calculate_average(numbers):\n",
    "    total = sum(numbers)\n",
    "    average = total / len(numbers)\n",
    "    return average\n",
    "\n",
    "# Test with empty list\n",
    "scores = []\n",
    "result = calculate_average(scores)\n",
    "print(f\"Average: {result}\")\n",
    "\n",
    "# Test with numbers\n",
    "scores = [85, 90, 78, 92, 88]\n",
    "result = calculate_average(scores)\n",
    "print(f\"Average: {result}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77e65c8a",
   "metadata": {},
   "source": [
    "### Bug 4: The Type Trouble\n",
    "```python\n",
    "age = input(\"Enter your age: \")\n",
    "next_year_age = age + 1\n",
    "print(f\"Next year you will be {next_year_age}\")\n",
    "```\n",
    "**Expected Behavior:** Should correctly calculate age + 1 (Hint: What type does `input()` return?)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1ad8f92",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Bug 4: Fix the code below\n",
    "# Error: \n",
    "\n",
    "age = input(\"Enter your age: \")\n",
    "next_year_age = age + 1\n",
    "print(f\"Next year you will be {next_year_age}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4eadcbb3",
   "metadata": {},
   "source": [
    "### Bug 5: The List Mystery\n",
    "```python\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "print(f\"The fourth fruit is: {fruits[3]}\")\n",
    "```\n",
    "**Expected Behavior:** Should handle the list access safely or explain why it fails"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "410029fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Bug 5: Fix the code below\n",
    "# Error: \n",
    "\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "print(f\"The fourth fruit is: {fruits[3]}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "814cfe98",
   "metadata": {},
   "source": [
    "---\n",
    "## __Reflection Questions__\n",
    "\n",
    "After completing the scavenger hunt, answer these questions:\n",
    "\n",
    "1. What resources did you use to find solutions? (Documentation, Stack Overflow, etc.)\n",
    "2. Which problem was most challenging? Why?\n",
    "3. What new Python feature or method did you find most interesting?\n",
    "4. How did you approach debugging when your code didn't work initially?\n",
    "\n",
    "**Write your answers in the markdown cell below:**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cf761bf",
   "metadata": {},
   "source": [
    "### Your Reflections:\n",
    "\n",
    "1. \n",
    "\n",
    "2. \n",
    "\n",
    "3. \n",
    "\n",
    "4. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be021ddd",
   "metadata": {},
   "source": [
    "---\n",
    "## Congratulations!\n",
    "\n",
    "You've completed the Lesson 04 Scavenger Hunt! You've practiced:\n",
    "- Researching Python documentation\n",
    "- Exploring new modules and methods\n",
    "- Problem-solving independently\n",
    "- Applying concepts in new contexts\n",
    "\n",
    "These skills are essential for becoming a successful programmer. Keep exploring!"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
