Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
302 changes: 241 additions & 61 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
@@ -1,63 +1,243 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f",
"metadata": {
"tags": []
},
"source": [
"# Lab | Flow Control"
]
"cells": [
{
"cell_type": "markdown",
"id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f",
"metadata": {
"tags": [],
"id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f"
},
"source": [
"# Lab | Flow Control"
]
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
"metadata": {
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400"
},
"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",
"source": [
"#Look at your code from the lab data structures, and improve repeated code with loops.\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"How many {product}s do you have? \"))\n",
" inventory[product] = quantity\n",
"\n",
"print(inventory)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Io0G1XKqS9d8",
"outputId": "6113fdd7-27ee-4ed5-85c4-3016181b5d1f"
},
"id": "Io0G1XKqS9d8",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"How many t-shirts do you have? 1\n",
"How many mugs do you have? 3\n",
"How many hats do you have? 5\n",
"How many books do you have? 2\n",
"How many keychains do you have? 5\n",
"{'t-shirt': 1, 'mug': 3, 'hat': 5, 'book': 2, 'keychain': 5}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#a. Prompt the user to enter the name of a product that a customer wants to order.\n",
"order_id = str(input(\"Enter the name of a product you want to order: \"))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hBpBRBuCV8Vz",
"outputId": "21c33d3a-f7ca-4d4b-c0a0-fd1065ed2959"
},
"id": "hBpBRBuCV8Vz",
"execution_count": 36,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the name of a product you want to order: hat\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#b. Add the product name to the \"customer_orders\" set.\n",
"customer_orders = set()"
],
"metadata": {
"id": "Yq4jqSQwelso"
},
"id": "Yq4jqSQwelso",
"execution_count": 37,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Ask if user wants another product\n",
"# Validate user answer\n",
"# while answer yes,\n",
" #ask for product\n",
" # add product\n",
" # ask if another proudct is desired\n",
" # validate user answer\n",
"\n",
"# If answer no,\n",
" # stop"
],
"metadata": {
"id": "3TgqkvWOmOA9"
},
"id": "3TgqkvWOmOA9",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#c. Ask the user if they want to add another product (yes/no).\n",
"another_product = input(\"Do you want to add another product? (yes/no): \")\n",
"\n",
"while another_product !=\"yes\" and another_product !=\"no\":\n",
"\n",
" print(\"Invalid input. Please enter (yes/no).\")\n",
" another_product = input(\"Do you want to add another product? (yes/no): \")\n",
"\n",
"while another_product == \"yes\":\n",
"\n",
" order_id = input(\"Enter the name of a product you want to order: \")\n",
" customer_orders.add(order_id)\n",
" another_product = input(\"Do you want to add another product? (yes/no): \")\n",
"\n",
" while another_product !=\"yes\" and another_product !=\"no\":\n",
"\n",
" print(\"Invalid input. Please enter (yes/no).\")\n",
" another_product = input(\"Do you want to add another product? (yes/no): \")\n",
"\n",
"\n",
"print(\"Thank you for you order! Your final order is: \", customer_orders)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1MYFIdslfDdh",
"outputId": "4f75a960-bc7a-4aec-b902-6a616b84ec55"
},
"id": "1MYFIdslfDdh",
"execution_count": 38,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Do you want to add another product? (yes/no): yes\n",
"Enter the name of a product you want to order: keychain\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter the name of a product you want to order: t-shirt\n",
"Do you want to add another product? (yes/no): no\n",
"Thank you for you order! Your final order is: {'keychain', 't-shirt'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#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\").\n",
"for product in inventory:\n",
" if product in customer_orders:\n",
" inventory[product] -= 1\n",
"\n",
"print(inventory)"
],
"metadata": {
"id": "JMfSC0P-spgI",
"outputId": "1538a1ce-ecdf-4a9c-a3fa-872ba0dd1a1b",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"id": "JMfSC0P-spgI",
"execution_count": 39,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'t-shirt': 0, 'mug': 1, 'hat': 3, 'book': 0, 'keychain': 4}\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.9.13"
},
"colab": {
"provenance": []
}
},
{
"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\")."
]
}
],
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
"nbformat": 4,
"nbformat_minor": 5
}