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
163 changes: 160 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,167 @@
"execution_count": null,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Choose the quantity of each product\n",
"Choose the quantity of mug\n"
]
}
],
"source": [
"# your code goes here"
"products = [\"mug\", \"hat\", \"book\"]\n",
"\n",
"def initialize_inventory(products):\n",
" print (\"Choose the quantity of each product\")\n",
" inventory = {}\n",
"\n",
" for i in products:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try : \n",
" \n",
" print(f\"Choose the quantity of {i}\")\n",
" product = int(input(\"Enter the quantity :\"))\n",
"\n",
" if product >= 0:\n",
" inventory[i] = product\n",
" valid_input = True\n",
" else :\n",
" raise ValueError (\"Please enter a positif number\")\n",
"\n",
" except ValueError as e:\n",
" print (f\"Error : {e}\")\n",
" \n",
"\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"\n",
"def get_customer_orders():\n",
" num = False\n",
" while not num :\n",
" \n",
" try :\n",
" number = int(input(\"Enter the number of customer_orders\"))\n",
"\n",
" if number > 0 :\n",
" num = True\n",
" if number < 0:\n",
" raise ValueError (\"Please enter a positif number\")\n",
" \n",
" \n",
" except ValueError as e:\n",
" print (f\"Error : {e}\")\n",
"\n",
"\n",
" \n",
" customer_orders = {}\n",
"\n",
" for i in range (1, number + 1):\n",
" product = False\n",
" while not product :\n",
" try :\n",
" prod = str(input(f\"choose product {i} : \")) \n",
" \n",
" if prod in inventory:\n",
" product = True\n",
" qty = False\n",
" while not qty :\n",
" try : \n",
" qté = int(input(\"choose a quantity : \"))\n",
"\n",
" if qté <= inventory[prod]:\n",
" qty = True\n",
" else:\n",
" raise ValueError (f\"You can't ask more than : {inventory[prod]}\")\n",
" \n",
" except ValueError as e:\n",
" print (f\"Error : {e}\")\n",
" \n",
" customer_orders[prod] = qté\n",
" \n",
" else:\n",
" raise ValueError (\"Product not found in the available products.\")\n",
" \n",
" except ValueError as e:\n",
" print(f\"Error : {e}\")\n",
" \n",
" return customer_orders\n",
" \n",
"customer_orders = get_customer_orders()\n",
"print (customer_orders)\n",
"print (inventory)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "b807e03c-98d4-42f0-bc63-b3768ce08699",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Nombre 1 : m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error : invalid literal for int() with base 10: 'm'\n",
"Block executed\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Nombre 1 : 8\n",
"Nombre 2 : m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error : invalid literal for int() with base 10: 'm'\n",
"Block executed\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Nombre 1 : 8\n",
"Nombre 2 : 18\n",
"Operateur *\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The result = 144\n",
"Block executed\n"
]
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "93be0af7-d714-4546-9e7e-f9cee7354126",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -66,7 +223,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down