|
1 | | -{ |
2 | | - "cells": [ |
3 | | - { |
4 | | - "cell_type": "markdown", |
5 | | - "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", |
6 | | - "metadata": { |
7 | | - "tags": [] |
8 | | - }, |
9 | | - "source": [ |
10 | | - "# Lab | Flow Control" |
11 | | - ] |
12 | | - }, |
13 | | - { |
14 | | - "cell_type": "markdown", |
15 | | - "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", |
16 | | - "metadata": {}, |
17 | | - "source": [ |
18 | | - "## Exercise: Managing Customer Orders Optimized\n", |
19 | | - "\n", |
20 | | - "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", |
21 | | - "\n", |
22 | | - "You did so without using flow control. Let's go a step further and improve this code.\n", |
23 | | - "\n", |
24 | | - "Follow the steps below to complete the exercise:\n", |
25 | | - "\n", |
26 | | - "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", |
27 | | - "\n", |
28 | | - "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", |
29 | | - " \n", |
30 | | - " a. Prompt the user to enter the name of a product that a customer wants to order.\n", |
31 | | - " \n", |
32 | | - " b. Add the product name to the \"customer_orders\" set.\n", |
33 | | - " \n", |
34 | | - " c. Ask the user if they want to add another product (yes/no).\n", |
35 | | - " \n", |
36 | | - " d. Continue the loop until the user does not want to add another product.\n", |
37 | | - "\n", |
38 | | - "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." |
39 | | - ] |
40 | | - } |
41 | | - ], |
42 | | - "metadata": { |
43 | | - "kernelspec": { |
44 | | - "display_name": "Python 3 (ipykernel)", |
45 | | - "language": "python", |
46 | | - "name": "python3" |
47 | | - }, |
48 | | - "language_info": { |
49 | | - "codemirror_mode": { |
50 | | - "name": "ipython", |
51 | | - "version": 3 |
52 | | - }, |
53 | | - "file_extension": ".py", |
54 | | - "mimetype": "text/x-python", |
55 | | - "name": "python", |
56 | | - "nbconvert_exporter": "python", |
57 | | - "pygments_lexer": "ipython3", |
58 | | - "version": "3.9.13" |
59 | | - } |
60 | | - }, |
61 | | - "nbformat": 4, |
62 | | - "nbformat_minor": 5 |
63 | | -} |
| 1 | +products = ["t-shirt", "mug", "hat", "book", "keychain"] |
| 2 | + |
| 3 | +inventory = {} |
| 4 | + |
| 5 | +for product in products: |
| 6 | + print ("Enter the quantity of each product available in the inventory for " + product + " : ") |
| 7 | + quantity = int(input()) |
| 8 | + inventory[product] = quantity |
| 9 | + |
| 10 | +customer_orders = set() |
| 11 | + |
| 12 | +while True: |
| 13 | + order = input("Enter product ordered: ").lower() |
| 14 | + |
| 15 | + if order in products: # On vérifie que le produit existe |
| 16 | + customer_orders.add(order) |
| 17 | + else: |
| 18 | + print("This product is not available in the store.") |
| 19 | + |
| 20 | + more = input("Do you want to add another product? (yes/no): ").lower() |
| 21 | + if more != "yes": |
| 22 | + break |
| 23 | + |
| 24 | +print("Products ordered:", customer_orders) |
| 25 | + |
| 26 | +total_products_ordered = len(customer_orders) |
| 27 | +percentage_ordered = (total_products_ordered / len(products)) * 100 |
| 28 | +order_status = (total_products_ordered, percentage_ordered) |
| 29 | + |
| 30 | +print("\nOrder Statistics:") |
| 31 | +print("Total Products Ordered:", order_status[0]) |
| 32 | +print("Percentage of Products Ordered:", round(order_status[1], 2), "%") |
| 33 | + |
| 34 | +for item in customer_orders: |
| 35 | + if item in inventory: |
| 36 | + inventory[item] -= 1 |
| 37 | + |
| 38 | +print("\nUpdated Inventory:") |
| 39 | +for product, quantity in inventory.items(): |
| 40 | + print(product, ":", quantity) |
0 commit comments