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
161 changes: 159 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,168 @@
"\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": "c705e408",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"\n",
"\n",
"# Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"#**Lists**: Ordered, mutable collections that store elements of different types and allow indexing, appending, removing, and modifying. [ ]\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)\n",
"\n",
"# Create an empty dictionary called `inventory`.\n",
"\n",
"#**Dictionaries**: Ordered (from Python 3.6), mutable key-value pairs for efficient lookup and organization of data. { }\n",
"\n",
"inventory = {} # create a new dict ----> assign curly brackets or inventory = dict()\n",
"\n",
"#1. Look at your code from the lab data structures, and improve repeated code with loops.\n",
"\n",
"for product in products:# for loop ----> take each product of the list\n",
"\n",
" quantity = int(\n",
" input(\n",
" f\"Enter the quantity of {product}: \"\n",
" )\n",
" )\n",
"\n",
" inventory[product] = quantity\n",
"\n",
"# Create an empty set called `customer_orders`.\n",
"\n",
"#**Sets**: Unordered, mutable collections of unique elements with set operations like union and intersection. { }\n",
"customer_orders = set()# To create an empty set -----> assign brackets ---> empty_set = ()"
]
},
{
"cell_type": "markdown",
"id": "d1d2811c",
"metadata": {},
"source": [
"2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "8a7e7552",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"no\n",
"{'mug', 'book', 'hat'}\n",
"3\n",
"your remaining stock is 37.0 %\n",
"60.0%\n"
]
}
],
"source": [
"\n",
"#Getting User Input\n",
"for _ in range(3): # for loop\n",
" #a. Prompt the user to enter the name of a product that a customer wants to order.\n",
" product_name = input(\"Enter a product name to order (e.g., t-shirt, mug, hat, book, keychain): \")\n",
"\n",
" #b. Add the product name to the \"customer_orders\" set.\n",
" customer_orders.add(product_name)#modifying a set ----> # Adding elements to a set e.g fruit.add('grape')\n",
" \n",
"another_product = \"yes\"\n",
"\n",
" #c. Ask the user if they want to add another product (yes/no).\n",
"while another_product == \"yes\":\n",
" another_product = input(\"Do you want add to another product?(yes/no): \").lower()\n",
"print(another_product)\n",
" #d. Continue the loop until the user does not want to add another product.\n",
"\n",
"# Print the products in the `customer_orders` set.\n",
"print(customer_orders) # Output: {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n",
"\n",
"# Calculate the following order statistics:\n",
" #- Total Products Ordered: The total number of products in the `customer_orders` set.\n",
" #- Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
" \n",
" #Store these statistics in a tuple called `order_status`.\n",
"\n",
"Total_Products_Ordered = len(customer_orders)#lenght----->len(fruits)\n",
"print(len(customer_orders))\n",
"\n",
"Percentage_of_Products_Ordered = (Total_Products_Ordered / len(products)) * 100\n",
"\n",
"#**Tuples**: Ordered, immutable collections commonly used for fixed values. ( )\n",
"\n",
"order_status = (Total_Products_Ordered+Percentage_of_Products_Ordered)# Creating a tuple----> name=()\n",
"\n",
"order_status2 = (100-order_status)\n",
"\n",
"print(f\"your remaining stock is {order_status2} %\")\n",
"\n",
"# Print the order statistics using the following format:\n",
" #Order Statistics:\n",
" #Total Products Ordered: <total_products_ordered>\n",
" #Percentage of Products Ordered: <percentage_ordered>%\n",
"\n",
"#total_products_ordered = len(customer_orders)\n",
"\n",
"percentage_ordered = (Total_Products_Ordered / len(products)) * 100\n",
"print(f\"{percentage_ordered}%\")"
]
},
{
"cell_type": "markdown",
"id": "38bd45e9",
"metadata": {},
"source": [
"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": 10,
"id": "bcb2b4bd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt : 2\n",
"mug : 3\n",
"hat : 4\n",
"book : 5\n",
"keychain : 6\n"
]
}
],
"source": [
"#inventory.items()# Key+values\n",
"\n",
"for key, value in inventory.items(): #for key, value in dic_name.items() ----> e.g for key, value in person.items(): print(key, \":\", value)\n",
" print(key, \":\", value)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +212,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down