Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

  • ๐Ÿ† 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 + v
sales = [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 + x
nums = [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 transactions

  • neg_avg: the average of all negative transactions

๐Ÿ“˜ Exampleยถ

transactions = [10, -5, -15, 30, 20]

  • pos_avg is 20 ((10 + 30 + 20) / 3) .

  • neg_avg is -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 for loop 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)