Utfordring: Hovedoppgave i Lagersortering
Flott arbeid med å mestre variabler, datatyper, slicing og konkatenasjon!
Nå skal du bruke de nye ferdighetene dine i et realistisk scenario hvor du organiserer nytt varelager for en dagligvarebutikk.
Oppgave
Swipe to start coding
Administrer et dagligvarelager ved å hente ut varenavn og kategorier ved hjelp av streng-slicing, tilordne priser og skrive ut formaterte utsagn.
- Bruk slicing på
items
-strengen for å hente ut:"Bubblegum"
→candy1
"Chocolate"
→candy2
"Pasta"
→dry_goods
- Bruk slicing på
categories
-strengen for å hente ut:"Candy Aisle"
→category1
"Pasta Aisle"
→category2
- Opprett prisvariabler:
bubblegum_price
="$1.50"
chocolate_price
="$2.00"
pasta_price
="$5.40"
- Bruk
print()
for å vise varenavn, priser og kategorier.
Krav til utdata
Skriv ut følgende:
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
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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)
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 2. Kapittel 7
single
9
1
2
3
# Lists of items and categories for slicing
items = "Bubblegum, Chocolate, Pasta"
categories = "Candy Aisle, Pasta Aisle"
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår