{ "cells": [ { "cell_type": "markdown", "id": "77447a22", "metadata": {}, "source": [ "# Lesson 04 activity: data types and operators\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", "**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", "**Your Task:**\n", "1. Save the `student_data` dictionary to a file \n", "2. Load the data back from the file into a new dictionary 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." ] }, { "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", "**Your Task:**\n", "1. Use a `try/except` block to access index `2` from `my_list`. Store the result in a variable called `item_2`.\n", "2. Use a `try/except` block to access index `10` from `my_list`. If the index doesn't exist, store `'Not found'` in a variable called `item_10`.\n", "3. Use a `try/except` block to access index `-1` from `my_list`. Store the result in a variable called `item_last`.\n", "4. Print all three variables.\n", "\n", "**Expected output:**\n", "```\n", "Item at index 2: cherry\n", "Item at index 10: Not found\n", "Item at index -1: elderberry\n", "```\n", "\n", "**Hint:** Use `IndexError` in your `except` clause." ] }, { "cell_type": "code", "execution_count": null, "id": "17edfb8a", "metadata": {}, "outputs": [], "source": [ "# Problem 3: Your solution here\n", "\n", "my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']\n", "\n", "# Access index 2 safely\n", "try:\n", " # Write your code here\n", " pass\n", "except IndexError:\n", " pass\n", "\n", "# Access index 10 safely (store 'Not found' if it doesn't exist)\n", "try:\n", " # Write your code here\n", " pass\n", "except IndexError:\n", " pass\n", "\n", "# Access index -1 safely\n", "try:\n", " # Write your code here\n", " pass\n", "except IndexError:\n", " pass\n", "\n", "print('Item at index 2:', item_2)\n", "print('Item at index 10:', item_10)\n", "print('Item at index -1:', item_last)\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\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!)" ] }, { "cell_type": "markdown", "id": "29a63bcc", "metadata": {}, "source": [ "### Bug 2\n", "\n", "**Expected behavior:** Should catch the error and print `Error: Cannot divide by zero!` without crashing" ] }, { "cell_type": "code", "execution_count": null, "id": "219901ae", "metadata": {}, "outputs": [], "source": [ "# Bug 2: Fix the code below\n", "# Error:\n", "\n", "numerator = 10\n", "denominator = 0\n", "\n", "try:\n", " result = numerator / denominator\n", " print(f'Result: {result}')\n", "\n", "except ValueError:\n", " print('Error: Cannot divide by zero!')\n" ] }, { "cell_type": "markdown", "id": "28bbe6fa", "metadata": {}, "source": [ "### Bug 3\n", "\n", "**Expected behavior:** Should handle an empty list gracefully without crashing" ] }, { "cell_type": "code", "execution_count": null, "id": "c6a0e1d8", "metadata": {}, "outputs": [], "source": [ "# Bug 3: Fix the code below\n", "# Error:\n", "\n", "scores = []\n", "\n", "total = sum(scores)\n", "average = total / len(scores)\n", "print(f'Average score: {average}')\n" ] }, { "cell_type": "markdown", "id": "77e65c8a", "metadata": {}, "source": [ "### Bug 4\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\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?" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }