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
100 changes: 99 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,104 @@
"\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": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"please enter the quantity of each product available.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt: 0\n",
"mug: 4\n",
"hat: 6\n",
"book: 4\n",
"keychain: 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 0, 'mug': 4, 'hat': 6, 'book': 4, 'keychain': 8}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"please enter three products that you want to purchase: t-shirt book book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered:\n",
"2Percentage of Products Ordered:50.00%\n",
"t-shirt is not available\n",
"updated inventory: {'t-shirt': 0, 'mug': 4, 'hat': 6, 'book': 2, 'keychain': 8}\n"
]
}
],
"source": [
"products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n",
"total_available_products=products.copy()\n",
"inventory={}\n",
"quantity=0\n",
"print(\"please enter the quantity of each product available.\")\n",
"for i in products:\n",
" quantity=int(input(i+\":\"))\n",
" while quantity<0:\n",
" quantity=int(input(\"please enter a new number that is not under 0:\"))\n",
" if quantity ==0:\n",
" total_available_products.remove(i)\n",
" inventory[i]=quantity\n",
"print(inventory)\n",
"\n",
"product_ordered_list=input(\"please enter three products that you want to purchase:\").split()\n",
"product_ordered={}\n",
"for i in product_ordered_list: \n",
" if i in product_ordered:\n",
" product_ordered[i]+=1\n",
" else:\n",
" product_ordered.update({i:1})\n",
"product_ordered\n",
" \n",
"customer_orders=set(product_ordered_list) & set(products)\n",
"total_products_ordered=len(customer_orders)\n",
"percentage_ordered=len(customer_orders)/len(total_available_products)\n",
"print(f\"Order Statistics:\\nTotal Products Ordered:{total_products_ordered}\\n Percentage of Products Ordered:{percentage_ordered:.2%}\")\n",
"\n",
"for j in product_ordered:\n",
" if not(j in inventory):\n",
" print(f\"there is no {j}\")\n",
" elif inventory[j]==0:\n",
" print(f\"{j} is not available\")\n",
" elif product_ordered[j]>inventory[j]:\n",
" print(f\"only {inventory[i]} {i} in stock\")\n",
" inventory[j]=0\n",
" else:\n",
" inventory[j]=inventory[j]-product_ordered[j]\n",
"print(\"updated inventory:\",inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -68,7 +166,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down