Lab

2.4.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: Variables and f-strings
# A help desk team tracks one short batch of support tickets.
# Create a variable named remaining_tickets.
# Create a variable named resolution_rate that stores the percent resolved today.
# Print exactly these three lines, using an f-string for each line:
# Team: data services
# Remaining tickets: 13
# Resolution rate: 27.8%
team = "data services"
open_tickets = 18
resolved_today = 5
escalated = 2
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
team = "data services"
open_tickets = 18
resolved_today = 5
escalated = 2

remaining_tickets = open_tickets - resolved_today
resolution_rate = resolved_today / open_tickets * 100

print(f"Team: {team}")
print(f"Remaining tickets: {remaining_tickets}")
print(f"Resolution rate: {resolution_rate:.1f}%")
Team: data services
Remaining tickets: 13
Resolution rate: 27.8%
### Question 2: Numeric operators
# The variables below describe a small order.
# Calculate subtotal, tax, and total.
# Calculate within_budget as a Boolean value.
# Print exactly these four lines:
# Subtotal: 51.0
# Tax: 4.21
# Total: 55.21
# Within budget: True
notebook_price = 12.50
pen_price = 8.75
sticker_price = 4.25
quantity = 2
tax_rate = 0.0825
budget = 60
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
notebook_price = 12.50
pen_price = 8.75
sticker_price = 4.25
quantity = 2
tax_rate = 0.0825
budget = 60

subtotal = (notebook_price + pen_price + sticker_price) * quantity
tax = round(subtotal * tax_rate, 2)
total = round(subtotal + tax, 2)
within_budget = total <= budget

print("Subtotal:", subtotal)
print("Tax:", tax)
print("Total:", total)
print("Within budget:", within_budget)
Subtotal: 51.0
Tax: 4.21
Total: 55.21
Within budget: True
### Question 3: Type conversion and built-in functions
# The values below are strings because they came from a web form.
# Convert each value to an integer.
# Use sum(), len(), max(), and type() to print exactly these four lines:
# Total responses: 46
# Average responses: 11.5
# Largest batch: 15
# Converted first value type: <class 'int'>
day1_text = "12"
day2_text = "9"
day3_text = "15"
day4_text = "10"
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
day1_text = "12"
day2_text = "9"
day3_text = "15"
day4_text = "10"

day1 = int(day1_text)
day2 = int(day2_text)
day3 = int(day3_text)
day4 = int(day4_text)
responses = [day1, day2, day3, day4]

print("Total responses:", sum(responses))
print("Average responses:", sum(responses) / len(responses))
print("Largest batch:", max(responses))
print("Converted first value type:", type(day1))
Total responses: 46
Average responses: 11.5
Largest batch: 15
Converted first value type: <class 'int'>
### Question 4: Strings and Boolean expressions
# Use the variables below to build a small account check.
# Create greeting by combining "Hello, " with first_name and "!".
# Create first_code as the first character in access_code.
# Create code_prefix as the first two characters in access_code.
# Create eligible as a Boolean expression that is True only when:
# account_active is True, login_count is at least 3, and access_code is not empty.
# Print exactly these four lines:
# Greeting: Hello, Maya!
# First code: A
# Code prefix: A7
# Eligible: True
first_name = "Maya"
access_code = "A7Z9"
account_active = True
login_count = 3
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
first_name = "Maya"
access_code = "A7Z9"
account_active = True
login_count = 3

greeting = "Hello, " + first_name + "!"
first_code = access_code[0]
code_prefix = access_code[0:2]
eligible = account_active and login_count >= 3 and bool(access_code)

print("Greeting:", greeting)
print("First code:", first_code)
print("Code prefix:", code_prefix)
print("Eligible:", eligible)
Greeting: Hello, Maya!
First code: A
Code prefix: A7
Eligible: True
### Question 5: Collection types
# Use a list, set, dictionary, and tuple to summarize the fruit data.
# Create unique_fruits by converting fruits to a set.
# Print the unique fruits with sorted(unique_fruits) so they appear as a list in alphabetical order.
# Create price_pair as a tuple containing "banana" and its price.
# Print exactly these four lines:
# Fruit count: 4
# Unique fruits: ['apple', 'banana', 'cherry']
# Banana price: 0.5
# Price pair: ('banana', 0.5)
fruits = ["apple", "banana", "apple", "cherry"]
fruit_prices = {"apple": 0.75, "banana": 0.50, "cherry": 1.25}
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
fruits = ["apple", "banana", "apple", "cherry"]
fruit_prices = {"apple": 0.75, "banana": 0.50, "cherry": 1.25}

unique_fruits = set(fruits)
price_pair = ("banana", fruit_prices["banana"])

print("Fruit count:", len(fruits))
print("Unique fruits:", sorted(unique_fruits))
print("Banana price:", fruit_prices["banana"])
print("Price pair:", price_pair)
Fruit count: 4
Unique fruits: ['apple', 'banana', 'cherry']
Banana price: 0.5
Price pair: ('banana', 0.5)