From 2ff58c28e2ac8d43c984bdf33a7db9ff7b7bc474 Mon Sep 17 00:00:00 2001 From: Rachel Vianna Date: Thu, 19 Feb 2026 09:55:06 +0100 Subject: [PATCH] Solved Lab: Flow Control Week 1 > Day 2 lab completed. --- .../lab-python-flow-control-checkpoint.ipynb | 287 ++++++++++++++++++ lab-python-flow-control.ipynb | 228 +++++++++++++- 2 files changed, 513 insertions(+), 2 deletions(-) create mode 100644 .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb new file mode 100644 index 0000000..3fd58d4 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,287 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "3a346774-c419-4336-ad80-364c90cb6079", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "22627c81-d06a-4ee4-84b4-8e44e8f0d2a9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 50\n", + "Please input the amount of mugs: 20\n", + "Please input the amount of hats: 30\n", + "Please input the amount of books: 10\n", + "Please input the amount of keychains: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 115\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the product you wish to order: keychain\n", + "Please input the product you wish to order: hat\n", + "Please input the product you wish to order: belt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, we don't have that!\n" + ] + } + ], + "source": [ + "# 1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " amount = int(input(f\"Please input the amount of {product}s: \"))\n", + " inventory[product] = amount \n", + "total_items = sum(inventory.values())\n", + "print(f\"The total number of items in stock is: {total_items}\")\n", + "\n", + "customer_orders = set()\n", + "for product in range(3):\n", + " orders = input(f\"Please input the product you wish to order:\")\n", + " if orders in products:\n", + " customer_orders.add(orders)\n", + " else:\n", + " print(\"Sorry, we don't have that!\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "e0035933-8f4f-4ac9-8335-c096d5bca03a", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of a product you wish to order: belt\n" + ] + } + ], + "source": [ + "# 2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + "\n", + "# a. Prompt the user to enter the name of a product that a customer wants to order.\n", + "customer_wish = input(\"Please enter the name of a product you wish to order:\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "897a1c12-279a-4bdd-99ed-87fc2a30b0d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'keychain', 't-shirt', 'mug', 'book', 'hat', 'belt'}\n" + ] + } + ], + "source": [ + "# b. Add the product name to the \"customer_orders\" set.\n", + "\n", + "customer_orders.add(customer_wish)\n", + "print(customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "f5b20b75-ee17-4e0a-86cf-aba1ed43b1bc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you wish to add another product to this order? (yes/no) Yes\n" + ] + } + ], + "source": [ + "# c. Ask the user if they want to add another product (yes/no).\n", + "\n", + "user_product_add = input(\"Do you wish to add another product to this order? (yes/no)\")" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "401df64a-1e3c-470d-b61e-f3075381226c", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you wish to add another product to this order? (yes/no): yes\n", + "Enter product: mug\n", + "Do you wish to add another product? (yes/no): yes\n", + "Enter product: keychain\n", + "Do you wish to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "# d. Continue the loop until the user does not want to add another product.\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "add_product = input(\"Do you wish to add another product to this order? (yes/no):\")\n", + "while add_product.lower() == \"yes\":\n", + " input(\"Enter product: \")\n", + " add_product = input(\"Do you wish to add another product? (yes/no): \")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "caf65903-9531-499c-96e9-8adcb25012bd", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 50\n", + "Please input the amount of mugs: 10\n", + "Please input the amount of hats: 20\n", + "Please input the amount of books: 100\n", + "Please input the amount of keychains: 80\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 260\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the product you wish to order: book\n", + "Please input the product you wish to order: mug\n", + "Please input the product you wish to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated mug: 9 remaining.\n", + "Updated hat: 19 remaining.\n", + "Updated book: 99 remaining.\n" + ] + } + ], + "source": [ + "# 3. Instead of updating the inventory by subtracting 1 from the quantity of each product,\n", + "# only do it for the products that were ordered (those in \"customer_orders\").\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " amount = int(input(f\"Please input the amount of {product}s: \"))\n", + " inventory[product] = amount \n", + "total_items = sum(inventory.values())\n", + "print(f\"The total number of items in stock is: {total_items}\")\n", + "\n", + "customer_orders = set()\n", + "for product in range(3):\n", + " orders = input(f\"Please input the product you wish to order:\")\n", + " if orders in products:\n", + " customer_orders.add(orders)\n", + " else:\n", + " print(\"Sorry, we don't have that!\")\n", + " \n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " print(f\"Updated {product}: {inventory[product]} remaining.\")\n", + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..3fd58d4 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "markdown", - "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "id": "3a346774-c419-4336-ad80-364c90cb6079", "metadata": {}, "source": [ "## Exercise: Managing Customer Orders Optimized\n", @@ -37,6 +37,230 @@ "\n", "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "22627c81-d06a-4ee4-84b4-8e44e8f0d2a9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 50\n", + "Please input the amount of mugs: 20\n", + "Please input the amount of hats: 30\n", + "Please input the amount of books: 10\n", + "Please input the amount of keychains: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 115\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the product you wish to order: keychain\n", + "Please input the product you wish to order: hat\n", + "Please input the product you wish to order: belt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, we don't have that!\n" + ] + } + ], + "source": [ + "# 1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " amount = int(input(f\"Please input the amount of {product}s: \"))\n", + " inventory[product] = amount \n", + "total_items = sum(inventory.values())\n", + "print(f\"The total number of items in stock is: {total_items}\")\n", + "\n", + "customer_orders = set()\n", + "for product in range(3):\n", + " orders = input(f\"Please input the product you wish to order:\")\n", + " if orders in products:\n", + " customer_orders.add(orders)\n", + " else:\n", + " print(\"Sorry, we don't have that!\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "e0035933-8f4f-4ac9-8335-c096d5bca03a", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of a product you wish to order: belt\n" + ] + } + ], + "source": [ + "# 2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + "\n", + "# a. Prompt the user to enter the name of a product that a customer wants to order.\n", + "customer_wish = input(\"Please enter the name of a product you wish to order:\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "897a1c12-279a-4bdd-99ed-87fc2a30b0d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'keychain', 't-shirt', 'mug', 'book', 'hat', 'belt'}\n" + ] + } + ], + "source": [ + "# b. Add the product name to the \"customer_orders\" set.\n", + "\n", + "customer_orders.add(customer_wish)\n", + "print(customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "f5b20b75-ee17-4e0a-86cf-aba1ed43b1bc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you wish to add another product to this order? (yes/no) Yes\n" + ] + } + ], + "source": [ + "# c. Ask the user if they want to add another product (yes/no).\n", + "\n", + "user_product_add = input(\"Do you wish to add another product to this order? (yes/no)\")" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "401df64a-1e3c-470d-b61e-f3075381226c", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you wish to add another product to this order? (yes/no): yes\n", + "Enter product: mug\n", + "Do you wish to add another product? (yes/no): yes\n", + "Enter product: keychain\n", + "Do you wish to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "# d. Continue the loop until the user does not want to add another product.\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "add_product = input(\"Do you wish to add another product to this order? (yes/no):\")\n", + "while add_product.lower() == \"yes\":\n", + " input(\"Enter product: \")\n", + " add_product = input(\"Do you wish to add another product? (yes/no): \")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "caf65903-9531-499c-96e9-8adcb25012bd", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 50\n", + "Please input the amount of mugs: 10\n", + "Please input the amount of hats: 20\n", + "Please input the amount of books: 100\n", + "Please input the amount of keychains: 80\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 260\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the product you wish to order: book\n", + "Please input the product you wish to order: mug\n", + "Please input the product you wish to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated mug: 9 remaining.\n", + "Updated hat: 19 remaining.\n", + "Updated book: 99 remaining.\n" + ] + } + ], + "source": [ + "# 3. Instead of updating the inventory by subtracting 1 from the quantity of each product,\n", + "# only do it for the products that were ordered (those in \"customer_orders\").\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " amount = int(input(f\"Please input the amount of {product}s: \"))\n", + " inventory[product] = amount \n", + "total_items = sum(inventory.values())\n", + "print(f\"The total number of items in stock is: {total_items}\")\n", + "\n", + "customer_orders = set()\n", + "for product in range(3):\n", + " orders = input(f\"Please input the product you wish to order:\")\n", + " if orders in products:\n", + " customer_orders.add(orders)\n", + " else:\n", + " print(\"Sorry, we don't have that!\")\n", + " \n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " print(f\"Updated {product}: {inventory[product]} remaining.\")\n", + " " + ] } ], "metadata": { @@ -55,7 +279,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,