Lab

2.6.2. Lab#

  • Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.

  • Sign in from the account menu before you start working on the assignment.

  • Write and run your code directly in the coding cells.

  • Multiple submissions are allowed. The highest score is kept.

### Question 1: Order Totals
# A manager is reviewing one customer order.
# Calculate subtotal, discount, and final_total from the variables below.
# Print exactly:
# Subtotal: 180
# Discount: 18.0
# Final total: 162.0
unit_price = 45
quantity = 4
discount_rate = 0.10
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
unit_price = 45
quantity = 4
discount_rate = 0.10

subtotal = unit_price * quantity
discount = subtotal * discount_rate
final_total = subtotal - discount

print("Subtotal:", subtotal)
print("Discount:", discount)
print("Final total:", final_total)
Subtotal: 180
Discount: 18.0
Final total: 162.0
### Question 2: Reorder Decision
# Use an if/else statement to decide whether the store should reorder.
# If inventory is less than reorder_point, set decision to "Reorder".
# Otherwise, set decision to "Wait".
# Print exactly:
# Decision: Reorder
inventory = 8
reorder_point = 10
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
inventory = 8
reorder_point = 10

if inventory < reorder_point:
    decision = "Reorder"
else:
    decision = "Wait"

print("Decision:", decision)
Decision: Reorder
### Question 3: High Sales List
# Use a loop and append() to build a list of sales above the target.
# Store the result in high_sales.
# Print exactly:
# High sales: [340, 410]
# First high sale: 340
sales = [340, 280, 410]
target = 300
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
sales = [340, 280, 410]
target = 300

high_sales = []
for sale in sales:
    if sale > target:
        high_sales.append(sale)

print("High sales:", high_sales)
print("First high sale:", high_sales[0])
High sales: [340, 410]
First high sale: 340
### Question 4: Department Counts
# Count how many employees appear in each department.
# Store the counts in a dictionary named department_counts.
# Print exactly:
# Sales count: 3
# HR count: 1
departments = ["Sales", "HR", "Sales", "Ops", "Sales"]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
departments = ["Sales", "HR", "Sales", "Ops", "Sales"]

department_counts = {}
for department in departments:
    department_counts[department] = department_counts.get(department, 0) + 1

print("Sales count:", department_counts["Sales"])
print("HR count:", department_counts["HR"])
Sales count: 3
HR count: 1
### Question 5: Reusable Margin Function
# Define a function named calculate_margin that takes revenue and cost.
# The function should return revenue minus cost.
# Call it with revenue 1250 and cost 875.
# Print exactly:
# Margin: 375
def calculate_margin(revenue, cost):
    ### Your code starts here.
    pass
    ### Your code ends here.

margin = calculate_margin(1250, 875)
print("Margin:", margin)
Margin: None

Hide code cell source

### Solution
def calculate_margin(revenue, cost):
    return revenue - cost

margin = calculate_margin(1250, 875)
print("Margin:", margin)
Margin: 375