๐ 20 points available
โ๏ธ Last updated on 9/4/2025
โถ๏ธ First, run the code cell below to import unittest, the module used for the ๐งญ Check Your Work boxes and the autograder.
# DO NOT MODIFY THE CODE IN THIS CELL
import unittest
tc = unittest.TestCase()๐ฏ Challenge 1: Create a Listยถ
๐ Tasksยถ
You have three job offers with salaries of 100000, 110000, and 120000.
Create a new list variable named offers that contains these three salary amounts (in the same order).
๐ก Hintยถ
my_list = [1, 2, 3] creates a list named my_list with initial values 1, 2, and 3.
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(offers[0])
print(offers[1])
print(offers[2])๐งญ Check Your Workยถ
Run the code cell below to test your solution.
โ๏ธ If the code cell runs without errors, youโre good to move on.
โ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'create-a-list'
_points = 2
tc.assertEqual(offers, [100000, 110000, 120000])๐ฏ Challenge 2: Number of NYX Productsยถ
๐ Tasksยถ
Assume you own 30 makeup products. The brands variable contains the list of brand names for each product in your collection. Can you count the number of NYX products in your collection and store the result in the num_NYX_products variable?
Use a for loop to perform this task.
brands = ["Bobbi Brown", "Maybelline", "Hourglass", "Clinique",
"NYX", "Kosas", "Bobbi Brown", "Hourglass", "NYX",
"NYX", "Clinique", "NYX", "Kosas", "Maybelline",
"Urban Decay", "Revlon", "Clinique", "NYX",
"Bobbi Brown", "Sephora", "Urban Decay", "NYX",
"Clinique", "NYX", "Estee Lauder", "Hourglass",
"Kosas", "NYX", "NYX", "Sephora"]
num_NYX_products = 0
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(f"You have {num_NYX_products} NYX products.")๐งญ Check Your Workยถ
Run the code cell below to test your solution.
โ๏ธ If the code cell runs without errors, youโre good to move on.
โ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'count-occurrences-in-a-list'
_points = 3
tc.assertEqual(num_NYX_products, 9)๐ฏ Challenge 3: Add All Values in a Listยถ
๐ Tasksยถ
Using a for loop, calculate the sum of all values in the list sales and store the result in the variable total_sales.
๐ก Hintยถ
The code below iterates through my_list and incrementally sums up the values in my_list.
my_list = [1, 2, 3, 4]
total = 0
for v in my_list:
total = total + vsales = [31, 30, 24, 20]
total_sales = 0
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(f"Total sales: {total_sales}")๐งญ Check Your Workยถ
Run the code cell below to test your solution.
โ๏ธ If the code cell runs without errors, youโre good to move on.
โ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'calculate-total-sales'
_points = 3
list_SOL = [31, 30, 24, 20]
tc.assertEqual(total_sales, sum(list_SOL))๐ฏ Challenge 4: Sum of Even Numbersยถ
๐ Tasksยถ
Using a for loop, find the sum of only even numbers in nums list and store the result to even_sum.
๐ Sample Codeยถ
my_list = [1, 2, 3, 4]
my_sum = 0
for x in my_list:
if x % 2 == 0:
my_sum = my_sum + xnums = [1, 2, 3, 4, 5, 6, 7, 8]
even_sum = 0
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(f"Sum of even numbers: {even_sum}")๐งญ Check Your Workยถ
Run the code cell below to test your solution.
โ๏ธ If the code cell runs without errors, youโre good to move on.
โ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'even-sum'
_points = 3
tc.assertEqual(even_sum, 20)๐ฏ Challenge 5: Total Salary of Chiefs Players ๐ยถ
๐ Tasksยถ
The variable players contains a list of 5 dictionaries. Each dictionary includes an NFL playerโs name, team, and salary.
Calculate the total salary of Kansas City Chiefs players ("team" == "Chiefs").
Store the result in the variable chiefs_total_salary.
Use a for loop to perform this task.
players = [
{
"name": "Tom Brady",
"team": "Buccaneers",
"salary": 23000000
},
{
"name": "Patrick Mahomes",
"team": "Chiefs",
"salary": 9900000
},
{
"name": "Aaron Donald",
"team": "Rams",
"salary": 19900000
},
{
"name": "Tyreek Hill",
"team": "Chiefs",
"salary": 18000000
},
{
"name": "Travis Kelce",
"team": "Chiefs",
"salary": 15000000
}
]
chiefs_total_salary = 0
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(f"Chiefs players' total salary is {chiefs_total_salary}!")๐งญ Check Your Workยถ
Run the code cell below to test your solution.
โ๏ธ If the code cell runs without errors, youโre good to move on.
โ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'chiefs-players-salary'
_points = 4
tc.assertAlmostEqual(chiefs_total_salary, 42900000.0)๐ฏ Challenge 6: Average Positive and Negative Transactionsยถ
๐ Tasksยถ
The transactions variable contains a list of transaction amounts. Your goal is to calculate the following two variables:
pos_avg: the average of all positive transactionsneg_avg: the average of all negative transactions
๐ Exampleยถ
transactions = [10, -5, -15, 30, 20]
pos_avgis20((10 + 30 + 20) / 3) .neg_avgis-10((-5 + -15) / 2).
โ๏ธ Rulesยถ
Create additional variables as needed for your calculations.
You may not manually count positives or negatives when dividing โ your code must handle the counting.
You must use a
forloop to solve this task.
transactions = [15, 8, 7, 5, 15, 72, -20, -66, 9, -4, -11, 21, 19]
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(f'Average of positive transactions is {pos_avg}')
print(f'Average of negative transactions is {neg_avg}')
๐งญ Check Your Workยถ
Run the code cell below to test your solution.
โ๏ธ If the code cell runs without errors, youโre good to move on.
โ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'average-pos-neg-transactions'
_points = 5
tc.assertAlmostEqual(pos_avg, 19.0)
tc.assertAlmostEqual(neg_avg, -25.25)