Skip to content
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

Lab python error handing Pilar Escrig #233

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
196 changes: 195 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,200 @@
"\n",
"4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n"
]
},
{
"cell_type": "code",
"execution_count": 166,
"id": "84683025",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory ={}\n",
" for product in products:\n",
" valid_quantity = False\n",
" while not valid_quantity:\n",
" try:\n",
" quantity = int(input(\"Which is the quantity of the product \" + product + \"?\"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n",
" valid_quantity = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" inventory[product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 167,
"id": "52f9032c",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
" valid_order = False\n",
" num_products = 0\n",
" while not valid_order:\n",
" try:\n",
" num_products = int(input(\"How many products do you order?\"))\n",
" if num_products < 0:\n",
" raise ValueError(\"Invalid order! Please enter a non-negative value.\")\n",
" valid_order = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" while len(customer_orders) < num_products: \n",
" try:\n",
" product = input(\"Which product do you want to add?\")\n",
" if product not in products:\n",
" raise ValueError(\"Invalid product! Please enter a valid product.\")\n",
" else: \n",
" customer_orders.add(product)\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 168,
"id": "bc4bc0ad",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(\n",
" '''\n",
" Order Statistics:\n",
" Total Products Ordered: {}\n",
" Percentage of Products Ordered: {}% \n",
" '''.format(order_statistics[0],order_statistics[1])\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 169,
"id": "0190ad95",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" inventory = {product: quantity - 1 if product in customer_orders else quantity for product, quantity in inventory.items()}\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 170,
"id": "0ed307bd",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(\"The quantity of the product {} is {}\".format(product,quantity))"
]
},
{
"cell_type": "code",
"execution_count": 171,
"id": "86ff6667",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = total_products_ordered/len(products)*100\n",
" return (total_products_ordered, percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 172,
"id": "6c3b0016",
"metadata": {},
"outputs": [],
"source": [
"def price(customer_orders):\n",
" prices = {}\n",
" for product in customer_orders:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" quantity = int(input(f\"What is the price of the {product}?\"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Invalid price! Please enter a valid price.\")\n",
" valid_price = True\n",
" prices[product] = quantity\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\") \n",
" return prices\n",
"\n",
"def print_price(prices):\n",
" for product, price in prices.items():\n",
" print(\"The price of the product {} is {}\".format(product,price))\n",
"def total_price(prices):\n",
" print(f\"Total price: {sum(prices.values())}\")"
]
},
{
"cell_type": "code",
"execution_count": 173,
"id": "f84ed5bb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: invalid literal for int() with base 10: 'g'\n",
"Error: Invalid quantity! Please enter a non-negative value.\n",
"Initial inventory:\n",
"The quantity of the product t-shirt is 6\n",
"The quantity of the product mug is 5\n",
"The quantity of the product hat is 4\n",
"The quantity of the product book is 7\n",
"The quantity of the product keychain is 5\n",
"Error: invalid literal for int() with base 10: 'g'\n",
"Error: Invalid order! Please enter a non-negative value.\n",
"Error: Invalid product! Please enter a valid product.\n",
"Error: Invalid product! Please enter a valid product.\n",
"\n",
" Order Statistics:\n",
" Total Products Ordered: 2\n",
" Percentage of Products Ordered: 40.0% \n",
" \n",
"Update inventory:\n",
"The quantity of the product t-shirt is 6\n",
"The quantity of the product mug is 5\n",
"The quantity of the product hat is 3\n",
"The quantity of the product book is 6\n",
"The quantity of the product keychain is 5\n",
"Error: invalid literal for int() with base 10: 'f'\n",
"Error: Invalid price! Please enter a valid price.\n",
"The price of the product hat is 4\n",
"The price of the product book is 5\n",
"Total price: 9\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"print(\"Initial inventory:\")\n",
"print_updated_inventory(inventory)\n",
"customer_orders = get_customer_orders(products)\n",
"inventory = update_inventory(customer_orders,inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders,inventory)\n",
"print_order_statistics(order_statistics)\n",
"print(\"Update inventory:\")\n",
"print_updated_inventory(inventory)\n",
"prices = price(customer_orders)\n",
"print_price(prices)\n",
"total_price(prices)"
]
}
],
"metadata": {
Expand All @@ -90,7 +284,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.6.2"
}
},
"nbformat": 4,
Expand Down