Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Haaste: Tuoteiden Liikevaihdot - Capstone | Funktiot
Johdatus Pythoniin

book
Haaste: Tuoteiden Liikevaihdot - Capstone

Käytetään tässä tehtävässä kaikkea, mitä olet oppinut tässä osiossa.

Tavoitteenasi on laskea kunkin tuotteen kokonaismyyntitulot ruokakaupassa tuotteiden hintojen ja myytyjen määrien perusteella.

Kun olet laskenut tuotekohtaiset tulot, sinun tulee lajitella tuotteet aakkosjärjestykseen ja esittää tulokset muotoillussa tulosteessa.

Tehtävä

Swipe to start coding

Laske ja esitä tuotteiden tuotot funktioiden avulla, tulokset selkeästi ja muotoillusti esitettynä.

  • Määrittele calculate_revenue(prices, quantities_sold):

    • Kerro jokainen prices- ja quantities_sold-listojen vastaava alkio keskenään;
    • Tallenna tulokset listaan nimeltä revenue ja palauta se.
  • Määrittele formatted_output(revenues):

    • Ottaa vastaan listan tupleja: (product_name, revenue);
    • Järjestä lista aakkosjärjestykseen tuotteen nimen mukaan;
    • Tulosta jokainen tuote ja sen tuotto määritellyssä muodossa.
  • Käytä calculate_revenue()-funktiota luodaksesi revenue-listan.

  • Käytä zip()-funktiota yhdistääksesi product_names ja revenue listaksi tupleja nimeltä revenue_per_product.

  • Kutsu formatted_output()-funktiota tulostaaksesi järjestetyt tulokset.

Tulostusvaatimukset

  • Jokaiselle tuotteelle tulosta:
    <product_name> has total revenue of $<revenue>

  • Varmista, että tuotteet ovat aakkosjärjestyksessä ennen tulostamista.

Ratkaisu

# Task 1: Define a function to calculate the revenue for each product
def calculate_revenue(prices, quantities_sold):
# Initialize a list to store the calculated revenues
revenue = []
# Iterate through the `prices` and `quantities_sold` lists using indexing
for index in range(len(prices)):
# Append the revenue (price * quantity) for each product to the revenue list
revenue.append(prices[index] * quantities_sold[index])
# Return the list of revenues
return revenue

# Task 2: Define a function to format and display the sorted revenues
def formatted_output(revenues):
for revenue in sorted(revenues):
# Print the formatted output for each product and its corresponding revenue
print(f"{revenue[0]} has total revenue of ${revenue[1]}")

# List of products, their prices, and the quantities sold
products = ["Bread", "Apples", "Oranges", "Bananas"]
prices = [0.50, 1.20, 2.50, 2.00] # price per item
quantities_sold = [150, 200, 100, 50] # number of items sold

# Task 3: Call the `calculate_revenue()` function to get total `revenue` for each product
revenue = calculate_revenue(prices, quantities_sold)

# Task 4: Use `zip()` to combine the products and their corresponding revenues into a list
revenue_per_product = list(zip(products, revenue))

# Task 5: Display the sorted revenues using the `formatted_output()` function
formatted_output(revenue_per_product)
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 6. Luku 7
# List of products, their prices, and the quantities sold
products = ["Bread", "Apples", "Oranges", "Bananas"]
prices = [0.50, 1.20, 2.50, 2.00] # price per item
quantities_sold = [150, 200, 100, 50] # number of items sold

Kysy tekoälyä

expand
ChatGPT

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

some-alt