Homework

2.6.3. Homework#

  • 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.

1. List Indexing

If departments = ["Sales", "HR", "Ops"], then departments[0] refers to "Sales".

Hide code cell source

### Solution
# True
# Python lists use zero-based indexing, so index 0 refers to the first item.

2. String and Number Operations

A dashboard import reads quantity as the string "12". Python will always treat "12" + 3 as the number 15 without conversion.

Hide code cell source

### Solution
# False
# The string must be converted with a function such as `int()` before numeric addition.

3. Conditional Business Rules

An inventory script uses an if statement to reorder only when inventory is below the reorder point. This is a reasonable use of conditional logic.

Hide code cell source

### Solution
# True
# The program chooses a branch based on whether the business condition is true.

4. Looping Through Sales

A for loop over a list of daily sales is useful when the same calculation should run once for each day.

Hide code cell source

### Solution
# True
# A `for` loop repeats work for each item in a collection.

5. Dictionary Keys

A dictionary that stores product prices must use only integer keys; text keys such as product names are invalid.

Hide code cell source

### Solution
# False
# Python dictionaries can use strings as keys, which is common for labels such as product names.
### 6. Format a Revenue Message
# Use the variables below to calculate profit and print exactly:
# East profit: 375
region = "East"
revenue = 1250
cost = 875

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
region = "East"
revenue = 1250
cost = 875

profit = revenue - cost
print(f"{region} profit: {profit}")
East profit: 375
### 7. Classify Customer Volume
# Use if/elif/else to classify the customer count.
# Set segment to "high" when customers is at least 100,
# "medium" when customers is at least 50, and "low" otherwise.
# Print exactly:
# Segment: medium
customers = 72

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
customers = 72

if customers >= 100:
    segment = "high"
elif customers >= 50:
    segment = "medium"
else:
    segment = "low"

print("Segment:", segment)
Segment: medium
### 8. Count Until Closing
# Use a while loop to add customer counts until the sentinel value 0 appears.
# Print exactly:
# Days counted: 2
# Customers before closing: 80
daily_customers = [42, 38, 0, 51]

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
daily_customers = [42, 38, 0, 51]

index = 0
days_counted = 0
customers_before_closing = 0
while daily_customers[index] != 0:
    customers_before_closing += daily_customers[index]
    days_counted += 1
    index += 1

print("Days counted:", days_counted)
print("Customers before closing:", customers_before_closing)
Days counted: 2
Customers before closing: 80
### 9. Count Product Categories
# Count each category in the purchases list using a dictionary.
# Print exactly:
# Software purchases: 3
# Service purchases: 2
purchases = ["Software", "Service", "Software", "Hardware", "Service", "Software"]

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
purchases = ["Software", "Service", "Software", "Hardware", "Service", "Software"]

category_counts = {}
for category in purchases:
    category_counts[category] = category_counts.get(category, 0) + 1

print("Software purchases:", category_counts["Software"])
print("Service purchases:", category_counts["Service"])
Software purchases: 3
Service purchases: 2
### 10. Reusable Revenue Per Employee
# Define revenue_per_employee(revenue, employees).
# Return revenue divided by employees.
# Print exactly:
# Revenue per employee: 250.0
def revenue_per_employee(revenue, employees):
    ### Your code starts here.
    pass
    ### Your code ends here.

metric = revenue_per_employee(5000, 20)
print("Revenue per employee:", metric)
Revenue per employee: None

Hide code cell source

### Solution
def revenue_per_employee(revenue, employees):
    return revenue / employees

metric = revenue_per_employee(5000, 20)
print("Revenue per employee:", metric)
Revenue per employee: 250.0