Haaste: Myynti ja Lähetykset
Tässä haasteessa hallinnoit päivittäisiä varastotasoja tärkeille ruokakaupan tuotteille viikon ajan. Käyttämällä range()
- ja len()
-funktioita yhdessä simuloit myynnin vaikutuksia varastoon.
Tehtävä
Swipe to start coding
Päivitä tuotteiden varastotasot myyntien ja toimitusten perusteella käyttäen loops.
- Käytä
for
-silmukkaa indeksin kanssa käydäksesi läpiproducts
-listan.- Jokaiselle tuotteelle vähennä myytyjen yksiköiden määrä (
units_sold
)products
:n varastosta.
- Jokaiselle tuotteelle vähennä myytyjen yksiköiden määrä (
- Käytä toista
for
-silmukkaa (myös indeksin kanssa) käydäksesiproducts
-listan uudelleen läpi.- Lisää vastaava arvo
shipment_received
-listasta varaston päivittämiseksi.
- Lisää vastaava arvo
Tulostusvaatimukset
- Myyntien käsittelyn jälkeen tulosta:
Stock after sales for <product_name>: <stock_level> units
- Toimitusten käsittelyn jälkeen tulosta:
Stock after shipment for <product_name>: <stock_level> units
- Lopuksi tulosta:
Final stock levels for all products: <products>
Ratkaisu
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# List of products with their initial stock levels at the start of the week
products = [
["Apples", 150],
["Bananas", 200],
["Oranges", 100],
["Mangoes", 120]
]
# List of products sold by the end of the week
units_sold = [["Apples", 30], ["Bananas", 45], ["Oranges", 20], ["Mangoes", 10]]
# New shipment received at the end of the week
shipment_received = [["Apples", 50], ["Bananas", 70], ["Oranges", 30], ["Mangoes", 40]]
# Task 1: Use a `for` loop to decrease stock levels based on sales
for item in range(len(products)):
products[item][1] -= units_sold[item][1] # Deduct the sold units from the stock
print(f"Stock after sales for {products[item][0]}: {products[item][1]} units")
# Task 2: Use a `for` loop to increase stock levels based on the new shipment
for item in range(len(products)):
products[item][1] += shipment_received[item][1] # Add the received shipment quantities
print(f"Stock after shipment for {products[item][0]}: {products[item][1]} units")
print("Final stock levels for all products:", products)
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 5. Luku 5
99
1
2
3
4
5
6
7
8
9
10
11
12
13
# List of products with their initial stock levels at the start of the week
products = [
["Apples", 150],
["Bananas", 200],
["Oranges", 100],
["Mangoes", 120]
]
# List of products sold by the end of the week
units_sold = [["Apples", 30], ["Bananas", 45], ["Oranges", 20], ["Mangoes", 10]]
# New shipment received at the end of the week
shipment_received = [["Apples", 50], ["Bananas", 70], ["Oranges", 30], ["Mangoes", 40]]
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme