Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Haaste: Myynti ja Lähetykset | Silmukat
Johdatus Pythoniin

book
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äpi products-listan.
    • Jokaiselle tuotteelle vähennä myytyjen yksiköiden määrä (units_sold) products:n varastosta.
  • Käytä toista for-silmukkaa (myös indeksin kanssa) käydäksesi products-listan uudelleen läpi.
    • Lisää vastaava arvo shipment_received-listasta varaston päivittämiseksi.

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

# 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ää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 5
# 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ä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt