Code Runner Challenge

popcorn 1

View IPYNB Source
# CODE_RUNNER: popcorn 1

# Step 1: make the hours list
hours = [3, 5, 2, 4]

# Step 2: start total at 0
total = 0

# Step 3: FOR EACH -> for ... in ...: add each h to total
for h in hours:
    total += h

# Step 4: LENGTH(hours) -> len(hours): compute the average
average = total / len(hours)

# Step 5: print total, then average
print(total)
print(average)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Runner Challenge

popcorn 2

View IPYNB Source
# CODE_RUNNER: popcorn 2

signed_up = [12, 7, 15, 9]   # Self-Defense, Parenting Skills, Fitness, Self-Sufficiency
capacity = 12

full = 0
for count in signed_up:
    if count >= capacity:     # Fixed: changed '>' to '>=' to include 12
        full = full + 1

print("Full workshops:", full)

# My prediction before running: Full workshops: 1
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Runner Challenge

popcorn 3

View IPYNB Source
# CODE_RUNNER: popcorn 3

workshops = ["Self-Defense", "Parenting Skills", "Fitness", "Self-Sufficiency"]
signed_up = [12, 7, 15, 9]     # signed_up[0] goes with workshops[0], and so on
capacity = 12

open_workshops = []             # COLLECT names instead of counting

for i in range(len(workshops)):         # i = 0, 1, 2, 3
    # TODO 1: replace pass with an if that is True when signed_up[i] < capacity
    if signed_up[i] < capacity:
        # TODO 2: inside your if, append workshops[i] to open_workshops
        open_workshops.append(workshops[i])

# TODO 3: print how many there are, then the list
print("Number of open workshops:", len(open_workshops))
print("Open workshops:", open_workshops)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ

MCQ 3.09: 4/4 answers: C,B,C,B

Code Runner Challenge

homework

View IPYNB Source
# CODE_RUNNER: homework

items    = ["Toothbrushes", "Blankets", "Socks", "Shampoo", "Notebooks"]
in_stock = [40, 8, 25, 5, 30]      # in the closet now
target   = [30, 20, 25, 15, 30]    # what staff want on hand

# Step 1: COLLECT items below target, and how many of each are needed
short_items = []
short_amounts = []
for i in range(len(items)):
    # TODO 1: if in_stock[i] < target[i], append items[i] and target[i] - in_stock[i]
    if in_stock[i] < target[i]:
        short_items.append(items[i])
        short_amounts.append(target[i] - in_stock[i])

# Step 2: SUM the shortages with a loop (no sum())
total_needed = 0
# TODO 2: loop through short_amounts and sum them up
for amount in short_amounts:
    total_needed += amount

# Step 3: FIND THE MAX shortage (track its position so you know the item's name)
# TODO 3: find the index of the largest shortage value
if len(short_amounts) > 0:
    max_index = 0
    for i in range(1, len(short_amounts)):
        if short_amounts[i] > short_amounts[max_index]:
            max_index = i
    most_urgent_item = short_items[max_index]
    max_shortage = short_amounts[max_index]

# Step 4: COMBINE: print "Fully stocked!" if nothing is short, otherwise the report
# TODO 4: print out the final formatted restock summary
if len(short_items) == 0:
    print("Fully stocked!")
else:
    print("Restock needed:")
    for i in range(len(short_items)):
        print(f"{short_items[i]}: need {short_amounts[i]}")
    print(f"Total items needed: {total_needed}")
    print(f"Most urgent: {most_urgent_item} (short by {max_shortage})")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...