Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Utmaning: Inventariesortering Slutprojekt | Variabler och Typer
Introduktion till Python

book
Utmaning: Inventariesortering Slutprojekt

Bra jobbat med att bemästra variabler, datatyper, slicing och konkatenering!

Nu ska vi tillämpa dina nya färdigheter på ett realistiskt scenario som handlar om att organisera ny lagerinventering i en livsmedelsbutik.

Uppgift

Swipe to start coding

Hantera ett livsmedelsbutikslager genom att extrahera varunamn och kategorier med hjälp av stränguppdelning, tilldela priser och skriva ut formaterade satser.

  • Dela upp strängen items för att extrahera:
    • "Bubblegum"candy1
    • "Chocolate"candy2
    • "Pasta"dry_goods
  • Dela upp strängen categories för att extrahera:
    • "Candy Aisle"category1
    • "Pasta Aisle"category2
  • Skapa prisvariabler:
    • bubblegum_price = "$1.50"
    • chocolate_price = "$2.00"
    • pasta_price = "$5.40"
  • Använd print() för att visa varunamn, priser och kategorier.

Utdatakrav

Skriv ut följande:

  • We have <candy1> for <bubblegum_price> in the <category1>
  • We have <candy2> for <chocolate_price> in the <category1>
  • We have <dry_goods> for <pasta_price> in the <category2>

Lösning

# Lists of items and categories for slicing
items = "Bubblegum, Chocolate, Pasta"
categories = "Candy Aisle, Pasta Aisle"

# Slice the `items` string into individual items
candy1 = items[:9] # Bubblegum
candy2 = items[11:20] # Chocolate
dry_goods = items[22:] # Pasta

# Slice the `categories` string into individual categories
category1 = categories[:11] # Candy Aisle
category2 = categories[13:] # Pasta Aisle

# Replace the blank placeholders (`___`) with the names of the variables
# to store the prices of each item
bubblegum_price = "$1.50" # Bubblegum costs 1.50 dollars
chocolate_price = "$2.00" # Chocolate costs 2.00 dollars
pasta_price = "$5.40" # Pasta costs 5.40 dollars

# Create print statements that combine the items, their prices, and categories
print("We have " + candy1 + " for " + bubblegum_price + " in the " + category1)
print("We have " + candy2 + " for " + chocolate_price + " in the " + category1)
print("We have " + dry_goods + " for " + pasta_price + " in the " + category2)
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 7
# Lists of items and categories for slicing
items = "Bubblegum, Chocolate, Pasta"
categories = "Candy Aisle, Pasta Aisle"

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt