Skip to content

Week 1_Lab 1_done #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
171 changes: 170 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,175 @@
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please input the quantity of product t-shirt: 10\n",
"Please input the quantity of product mug: 10\n",
"Please input the quantity of product hat: 10\n",
"Please input the quantity of product book: 10\n",
"Please input the quantity of product keychain: 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n"
]
}
],
"source": [
"# create a product list, inventory dictionary\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {} \n",
"\n",
"for p in products:\n",
" quantity = int(input(f\"Please input the quantity of product {p}:\"))\n",
" inventory[p] = quantity\n",
" \n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please input the name of product that a customer wants to order: mug\n",
"Please input the name of product that a customer wants to order: hat\n",
"Please input the name of product that a customer wants to order: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'mug', 'book'}\n"
]
}
],
"source": [
"# create a customer order set\n",
"\n",
"customer_orders = set()\n",
" \n",
"for i in range(3):\n",
" product = input(\"Please input the name of product that a customer wants to order:\")\n",
" customer_orders.add(product)\n",
"\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ---------------------------------------only execute below codes---------------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
" Total Products Ordered: 3\n",
" Percentage of Products Ordered: 60% \n"
]
}
],
"source": [
"# create 0rder_Stats tuple\n",
"\n",
"Total_Products_Ordered = len(customer_orders)\n",
"Percentage_of_Products_Ordered = (Total_Products_Ordered/len(inventory))\n",
"\n",
"order_status = (Total_Products_Ordered,Percentage_of_Products_Ordered)\n",
"\n",
"print(f\"Order Statistics:\\n Total Products Ordered: {order_status[0]}\\n Percentage of Products Ordered: {order_status[1]*100:.0f}% \") \n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 10}\n"
]
}
],
"source": [
"# Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"\n",
"for o in customer_orders:\n",
" inventory[o] -= 1\n",
" \n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"updated inventory overview:\n",
"\n",
"t-shirt 10\n",
"mug 9\n",
"hat 9\n",
"book 9\n",
"keychain 10\n"
]
}
],
"source": [
"# Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"\n",
"print(f\"updated inventory overview:\\n\")\n",
"\n",
"for k,v in inventory.items(): \n",
" print(f\"{k} {v}\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -68,7 +237,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down