Homework

3.3.3. Homework#

  • Use these questions after class to check your understanding of the chapter concepts and coding skills.

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

  • Complete all true/false and coding questions before you submit the homework.

  • Launch Live Code (the rocket) to write and run code in the coding cells.

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

1. Branching

A product manager says a rule should send active premium customers to priority review and all other customers to standard review. An if/else statement is an appropriate way to express that decision.

Hide code cell source

### Solution
# True
# An if/else statement chooses between exactly two paths. The condition (active and
# premium) sends matching customers to priority review, and else catches every other
# customer.

2. Comparison Operators

An analyst can use score >= 90 to produce a Boolean value before deciding whether a record receives an honors label.

Hide code cell source

### Solution
# True
# A comparison such as score >= 90 evaluates to the Boolean True or False. You can store
# that result or use it directly as the condition of an if statement.

3. Loop Accumulators

A reporting script that totals only approved invoices can use a loop with an accumulator that increases when each invoice qualifies.

Hide code cell source

### Solution
# True
# An accumulator starts at 0 before the loop, and an if inside the loop adds each invoice
# amount only when the invoice is approved. After the loop, the accumulator holds the
# total of approved invoices.

4. While Loops

A while loop automatically stops after one pass even when its condition remains true.

Hide code cell source

### Solution
# False
# A while loop checks its condition before every pass and keeps repeating as long as the
# condition is True. It stops only when the condition becomes False or a break runs, so a
# condition that stays true creates an infinite loop.

5. Conditional Expressions

A conditional expression is best used for large multi-step workflows with many statements in each branch.

Hide code cell source

### Solution
# False
# A conditional expression (a if condition else b) chooses between two values in a single
# expression. Multi-step branches with several statements belong in a full if/elif/else
# statement.
### 6. Route a service ticket
# Use `if`, `elif`, and `else` to classify the ticket below with these rules:
# - If age_days is at least 7 and customer_tier is "gold", the priority is "urgent".
# - Otherwise, if age_days is at least 7, the priority is "high".
# - Otherwise, the priority is "normal".
# Print exactly these two lines:
# # Ticket age: 9
# # Priority: urgent
age_days = 9
customer_tier = "gold"

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
age_days = 9
customer_tier = "gold"

if age_days >= 7 and customer_tier == "gold":
    priority = "urgent"
elif age_days >= 7:
    priority = "high"
else:
    priority = "normal"

print("Ticket age:", age_days)
print("Priority:", priority)
Ticket age: 9
Priority: urgent
### 7. Count qualifying sales
# Use a `for` loop and an accumulator to count sales of at least 500. Print exactly these two lines:
# # Qualifying sales: 3
# # Qualifying total: 2190
sales = [320, 750, 120, 810, 630]

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
sales = [320, 750, 120, 810, 630]
qualifying_count = 0
qualifying_total = 0

for sale in sales:
    if sale >= 500:
        qualifying_count += 1
        qualifying_total += sale

print("Qualifying sales:", qualifying_count)
print("Qualifying total:", qualifying_total)
Qualifying sales: 3
Qualifying total: 2190
### 8. Stop at a budget limit
# Use a `while` loop to add weekly savings until the balance reaches at least 500. Print exactly these two lines:
# # Weeks: 6
# # Balance: 540
balance = 180
weekly_savings = 60

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
balance = 180
weekly_savings = 60
weeks = 0

while balance < 500:
    balance += weekly_savings
    weeks += 1

print("Weeks:", weeks)
print("Balance:", balance)
Weeks: 6
Balance: 540
### 9. Create pass/fail labels
# Use a loop and a conditional expression to build the labels list.
# A score of 75 or higher gets "pass"; any lower score gets "review". Print exactly this line:
# # Labels: ['pass', 'review', 'pass', 'review']
scores = [84, 62, 91, 70]

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
scores = [84, 62, 91, 70]
labels = []

for score in scores:
    labels.append("pass" if score >= 75 else "review")

print("Labels:", labels)
Labels: ['pass', 'review', 'pass', 'review']
### 10. Build a multiplication row
# Use a loop to create products for 4 times 1 through 5. Print exactly this line:
# # Row: [4, 8, 12, 16, 20]
factor = 4

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
factor = 4
row = []

for value in range(1, 6):
    row.append(factor * value)

print("Row:", row)
Row: [4, 8, 12, 16, 20]