From 05f69582cdbc5c53374e363e48688b508b351a62 Mon Sep 17 00:00:00 2001 From: Katarzyna Grzyb Date: Tue, 16 Jun 2026 16:51:53 +0200 Subject: [PATCH] Complete lab2 --- .../lab-python-flow-control-checkpoint.ipynb | 158 ++++++++++++++++++ lab-python-flow-control.ipynb | 61 ++++++- 2 files changed, 218 insertions(+), 1 deletion(-) 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..422554a --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,158 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "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": 5, + "id": "f2e740d1-5575-4d8e-99c1-e450c17c8cb6", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "What´s the quantity of t-shirt? 6\n", + "What´s the quantity of mug? 9\n", + "What´s the quantity of hat? 7\n", + "What´s the quantity of book? 2\n", + "What´s the quantity of keychain? 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What do you want to order?\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: book\n", + "Do you want to order anything else? Type Yes or No. yes\n", + "Enter a product name: mug\n", + "Do you want to order anything else? Type Yes or No. no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you for your order.\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "customer_orders = set()\n", + "for product in products:\n", + " outcome=input(f\"What´s the quantity of {product}?\")\n", + " inventory[product]=int(outcome)\n", + "\n", + "print(\"What do you want to order?\")\n", + "user_choice2 = \"yes\"\n", + "while user_choice2 == \"yes\":\n", + " user_choice = input(\"Enter a product name: \")\n", + " if user_choice in products:\n", + " customer_orders.add(user_choice)\n", + " else:\n", + " print(\"Please enter valid product from the list.\")\n", + " user_choice2 = input(\"Do you want to order anything else? Type Yes or No.\").strip().lower()\n", + "else: \n", + " print(\"Thank you for your order.\") \n", + " \n", + "for product in customer_orders:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "58387a11-fb71-4ff7-80cc-ff3cf5878caf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 8, 'hat': 7, 'book': 1, 'keychain': 1}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "069141b5-c738-4242-81f3-0b6cef0b562d", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..71635d1 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,65 @@ "\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": null, + "id": "f2e740d1-5575-4d8e-99c1-e450c17c8cb6", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "customer_orders = set()\n", + "for product in products:\n", + " outcome=input(f\"What´s the quantity of {product}?\")\n", + " inventory[product]=int(outcome)\n", + "\n", + "print(\"What do you want to order?\")\n", + "user_choice2 = \"yes\"\n", + "while user_choice2 == \"yes\":\n", + " user_choice = input(\"Enter a product name: \")\n", + " if user_choice in products:\n", + " customer_orders.add(user_choice)\n", + " else:\n", + " print(\"Please enter valid product from the list.\")\n", + " user_choice2 = input(\"Do you want to order anything else? Type Yes or No.\").strip().lower()\n", + "else: \n", + " print(\"Thank you for your order.\") \n", + " \n", + "for product in customer_orders:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "58387a11-fb71-4ff7-80cc-ff3cf5878caf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 8, 'hat': 7, 'book': 1, 'keychain': 1}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "069141b5-c738-4242-81f3-0b6cef0b562d", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +114,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,