diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..5cdae32 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,244 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "d2819b47", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "products= [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"] #global variable can call inside\n", + "inventory ={}\n", + "def initialize_inventory (products):\n", + " #Variables does not exists outside the function\n", + " for i in products:\n", + " \n", + " qty=int(input(f\"Enter the quantity of {i}:\"))\n", + " if qty<0:\n", + " \n", + " print(\"Please enter a positive number\")\n", + " inventory[i]=qty\n", + " \n", + " return inventory\n", + "initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "7b008429", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No more items added\n", + "{'hat', 'book', 'mug'}\n" + ] + } + ], + "source": [ + "customer_orders =set()\n", + "def get_customer_orders():\n", + " while True:\n", + " add_item =input(\"Which item do you want to add to your cart?\").lower()\n", + " customer_orders.add(add_item)\n", + " last_add=input (\"Do you want to add something more?(yes/no)\").strip().lower()\n", + " is_true=(last_add==\"yes\")\n", + " if not is_true:\n", + " print(\"No more items added\")\n", + " break\n", + " return customer_orders\n", + "customer_orders=get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "caa347fd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uptdated inventory: {'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "def update_inventory (customers_orders, inventory):\n", + " for item in customers_orders:\n", + " if item in inventory:\n", + " \n", + " inventory[item]-=1\n", + " \n", + " return inventory\n", + "update_inventory(customer_orders, inventory)\n", + "print(\"Uptdated inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "f420275b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The percentage is 60.0%\n", + "(60.0,)\n", + "Order Statistics:\n", + "Total Products Ordered:3\n", + "Percentage of Products Ordered:60.0%\n" + ] + }, + { + "data": { + "text/plain": [ + "(60.0,)" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_order_statistics (customer_orders, products):\n", + " total_products_ordered=len(customer_orders)\n", + " percentage= len(customer_orders)/len(products)*100\n", + " print( f\"The percentage is {percentage}%\")\n", + " order_status=(percentage,)\n", + " print(order_status)\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered:{total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered:{percentage}%\")\n", + " return order_status\n", + "calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "f2a89c1d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt 5\n", + "mug 4\n", + "hat 4\n", + "book 4\n", + "keychain 5\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 5}" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "def print_update_inventory(inventory):\n", + " for key, value in inventory.items():\n", + " print(key,value)\n", + " return inventory\n", + "print_update_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "f12f84dd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + }, + { + "ename": "KeyboardInterrupt", + "evalue": "Interrupted by user", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mKeyboardInterrupt\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[21]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28mprint\u001b[39m(inventory)\n\u001b[32m 2\u001b[39m customer_orders= \u001b[38;5;28mset\u001b[39m()\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m my_list=\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mWhich item do you want? t-shirt, mug, hat, book or keychain?\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m.lower()\n\u001b[32m 4\u001b[39m customer_orders.add(my_list)\n\u001b[32m 5\u001b[39m \u001b[38;5;28mprint\u001b[39m(customer_orders)\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Roaming\\Python\\Python314\\site-packages\\ipykernel\\kernelbase.py:1275\u001b[39m, in \u001b[36mKernel.raw_input\u001b[39m\u001b[34m(self, prompt)\u001b[39m\n\u001b[32m 1273\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 1274\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[32m-> \u001b[39m\u001b[32m1275\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_input_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 1276\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1277\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_parent_ident\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mshell\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1278\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mget_parent\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mshell\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1279\u001b[39m \u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 1280\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Roaming\\Python\\Python314\\site-packages\\ipykernel\\kernelbase.py:1320\u001b[39m, in \u001b[36mKernel._input_request\u001b[39m\u001b[34m(self, prompt, ident, parent, password)\u001b[39m\n\u001b[32m 1317\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[32m 1318\u001b[39m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[32m 1319\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mInterrupted by user\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m1320\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m 1321\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[32m 1322\u001b[39m \u001b[38;5;28mself\u001b[39m.log.warning(\u001b[33m\"\u001b[39m\u001b[33mInvalid Message:\u001b[39m\u001b[33m\"\u001b[39m, exc_info=\u001b[38;5;28;01mTrue\u001b[39;00m)\n", + "\u001b[31mKeyboardInterrupt\u001b[39m: Interrupted by user" + ] + } + ], + "source": [ + "print(inventory)\n", + "customer_orders= set()\n", + "my_list=input(\"Which item do you want? t-shirt, mug, hat, book or keychain?\").lower()\n", + "customer_orders.add(my_list)\n", + "print(customer_orders)\n", + "add_order=input(\"Do you want to add another product? (yes/no)\").strip().lower()\n", + "add_order=(add_order==\"yes\")\n", + "while True:\n", + " add_item=input(\"Which item do you want to add?\").lower()\n", + " customer_orders.add(add_item)\n", + " print(f\"You just added a {add_item}\")\n", + " last_add=input (\"Do you want to add something more?(yes/no)\").strip().lower()\n", + " is_true=(last_add==\"yes\")\n", + " #last_add se convierte en bool al hacerlo ==\"yes\"\n", + " if not is_true:\n", + " print(\"No more items added\")\n", + " break\n", + " \n", + "print(customer_orders)\n", + "total_products_ordered=len(customer_orders)\n", + "print(total_products_ordered)\n", + "percentage= len(customer_orders)/len(products)*100\n", + "print( f\"The percentage is {percentage}%\")\n", + "order_status=(percentage,)\n", + "print(order_status)\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered:{total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered:{percentage}%\")\n", + "\n", + "for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item]-=1\n", + "\n", + "for key, value in inventory.items():\n", + " print(key,value)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +294,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.0" } }, "nbformat": 4,