Lab

3.3.2. Lab#

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

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

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

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

### Question 1: Unit conversion with modulus and floor division
# An app tracks a user's total screen time in minutes for the month.
# Convert total_minutes into days, hours, and minutes using // and %.
# Print exactly these three lines:
# Days: 2
# Hours: 18
# Minutes: 40
total_minutes = 4000
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
total_minutes = 4000

days = total_minutes // 1440
remainder = total_minutes % 1440
hours = remainder // 60
minutes = remainder % 60

print(f"Days: {days}")
print(f"Hours: {hours}")
print(f"Minutes: {minutes}")
Days: 2
Hours: 18
Minutes: 40
### Question 2: Nested conditionals for a loyalty tier
# A store assigns a loyalty tier based on account status and spending.
# If account_active is False, tier is "Inactive".
# Otherwise, use nested conditionals on total_spent:
# >= 1000 -> "Platinum", >= 500 -> "Gold", >= 100 -> "Silver", else "Bronze".
# Print exactly these two lines:
# Customer: Jordan Lee
# Tier: Gold
customer_name = "Jordan Lee"
account_active = True
total_spent = 640.50
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
customer_name = "Jordan Lee"
account_active = True
total_spent = 640.50

if not account_active:
    tier = "Inactive"
else:
    if total_spent >= 1000:
        tier = "Platinum"
    elif total_spent >= 500:
        tier = "Gold"
    elif total_spent >= 100:
        tier = "Silver"
    else:
        tier = "Bronze"

print(f"Customer: {customer_name}")
print(f"Tier: {tier}")
Customer: Jordan Lee
Tier: Gold
### Question 3: For-loop accumulator with a condition
# daily_sales lists a store's revenue for each day this week.
# Loop through daily_sales. For every day that is at least sales_target,
# add 1 to qualifying_days and add the day's sales to total_qualifying_sales.
# Print exactly these two lines:
# Qualifying days: 3
# Total qualifying sales: 1760
daily_sales = [420, 150, 610, 90, 730, 260]
sales_target = 300
qualifying_days = 0
total_qualifying_sales = 0
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
daily_sales = [420, 150, 610, 90, 730, 260]
sales_target = 300
qualifying_days = 0
total_qualifying_sales = 0

for sale in daily_sales:
    if sale >= sales_target:
        qualifying_days += 1
        total_qualifying_sales += sale

print(f"Qualifying days: {qualifying_days}")
print(f"Total qualifying sales: {total_qualifying_sales}")
Qualifying days: 3
Total qualifying sales: 1760
### Question 4: While loop until a savings goal is reached
# Each month, savings_total grows by growth_rate and then monthly_deposit is added.
# Use a while loop that repeats while savings_total is below goal.
# Count the number of months in the variable months.
# Print exactly these two lines (savings formatted to 2 decimal places):
# Months needed: 5
# Final savings: $2071.21
savings_total = 1000
monthly_deposit = 200
growth_rate = 0.01
goal = 2000
months = 0
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
savings_total = 1000
monthly_deposit = 200
growth_rate = 0.01
goal = 2000
months = 0

while savings_total < goal:
    savings_total = savings_total * (1 + growth_rate) + monthly_deposit
    months += 1

print(f"Months needed: {months}")
print(f"Final savings: ${savings_total:.2f}")
Months needed: 5
Final savings: $2071.21
### Question 5: Conditional expression for shipping fee
# An order qualifies for free shipping once order_total reaches free_shipping_threshold.
# Use a single conditional expression (ternary) to set shipping_fee:
# 0 if the order qualifies, otherwise 5.99.
# Print exactly these two lines (both formatted to 2 decimal places):
# Order total: $42.50
# Shipping fee: $5.99
order_total = 42.50
free_shipping_threshold = 50
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
order_total = 42.50
free_shipping_threshold = 50

shipping_fee = 0 if order_total >= free_shipping_threshold else 5.99

print(f"Order total: ${order_total:.2f}")
print(f"Shipping fee: ${shipping_fee:.2f}")
Order total: $42.50
Shipping fee: $5.99