- The challenges here are optional and are not graded.
- Use these challenges to prepare yourself for the upcoming quiz.
- You will have 25 minutes to complete two questions that are similar to the ones shown here.
▶️ First, run the code cell below to import unittest, a module used for 🧭 Check Your Work sections and the autograder.
# DO NOT MODIFY THE CODE IN THIS CELL
import unittest
tc = unittest.TestCase()🎯 Challenge 1: Average Number of Reviews using a For Loop¶
You are given a list-type variable named products, which contains a list of dictionaries. Each dictionary in the list contains the brand, number of reviews, and name of a beauty product on Sephora.com. 💄
👇 Tasks¶
- Use a
forloop to find the average number of reviews of Laneige products (products where"brand"is"Laneige"). - Store the result to a new variable named
avg_num_reviews.
📤 Expected Output¶
Laneige products have 6084.0 reviews on average.my_products = [
{"brand": "Laneige", "num_reviews": 12715, "name": "Lip Sleeping Mask"},
{"brand": "The Ordinary", "num_reviews": 2274, "name": "Hyaluronic Acid 2% + B5"},
{"brand": "Laneige", "num_reviews": 2771, "name": "Water Sleeping Mask"},
{"brand": "The Ordinary", "num_reviews": 3952, "name": "Niacinamide 10% + Zinc 1%"},
{
"brand": "Chanel",
"num_reviews": 528,
"name": "CHANCE EAU TENDRE Eau de Toilette",
},
{"brand": "Laneige", "num_reviews": 2766, "name": "Lip Glowy Balm"},
{"brand": "Chanel", "num_reviews": 724, "name": "COCO MADEMOISELLE Eau de Parfum"},
]
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(f"Laneige products have {avg_num_reviews} reviews on average.")🧭 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-number-of-reviews-using-a-for-loop"
_points = 10
_obfuscate = False
tc.assertAlmostEqual(avg_num_reviews, 6084)🎯 Challenge 2: Average Number of Reviews using a Function¶
Let’s make your logic from the previous question reusable by creating a function.
👇 Task¶
Create a function named get_avg_num_reviews() that returns the average number of reviews of products for a given brand.
The get_avg_num_reviews function takes the following two parameters:
products- This is alistof dictionaries. Each dictionary in the list contains the brand, number of reviews, and name of a beauty product.brand- This is a string value containing the brand name (e.g.,'Laneige').
If there is no matching brand in the products list, return -1.
💡 Example¶
get_avg_num_reviews(my_products, "Laneige") should return the average number of reviews of Laneige products in my_products list.
📤 Expected Output¶
6084.0
3113.0
-1
626.0
11923.0
-1my_products = [
{"brand": "Laneige", "num_reviews": 12715, "name": "Lip Sleeping Mask"},
{"brand": "The Ordinary", "num_reviews": 2274, "name": "Hyaluronic Acid 2% + B5"},
{"brand": "Laneige", "num_reviews": 2771, "name": "Water Sleeping Mask"},
{"brand": "The Ordinary", "num_reviews": 3952, "name": "Niacinamide 10% + Zinc 1%"},
{
"brand": "Chanel",
"num_reviews": 528,
"name": "CHANCE EAU TENDRE Eau de Toilette",
},
{"brand": "Laneige", "num_reviews": 2766, "name": "Lip Glowy Balm"},
{"brand": "Chanel", "num_reviews": 724, "name": "COCO MADEMOISELLE Eau de Parfum"},
]
your_products = [
{"brand": "NARS", "num_reviews": 11923, "name": "Radiant Creamy Concealer"}
]
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(get_avg_num_reviews(my_products, "Laneige"))
print(get_avg_num_reviews(my_products, "The Ordinary"))
print(
get_avg_num_reviews(my_products, "Sulwhasoo")
) # edge case - no matching brand, should return -1
print(get_avg_num_reviews(my_products, "Chanel"))
print(get_avg_num_reviews(your_products, "NARS"))
print(
get_avg_num_reviews([], "First Aid Beauty")
) # edge case - empty, should return -1🧭 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-price-of-sneakers-using-a-function"
_points = 15
_obfuscate = False
tc.assertAlmostEqual(get_avg_num_reviews(my_products, "Laneige"), 6084.0)
tc.assertAlmostEqual(get_avg_num_reviews(my_products, "The Ordinary"), 3113.0)
tc.assertAlmostEqual(get_avg_num_reviews(my_products, "Chanel"), 626.0)
tc.assertAlmostEqual(get_avg_num_reviews(my_products, "JLo Beauty"), -1)
tc.assertAlmostEqual(get_avg_num_reviews(your_products, "NARS"), 11923.0)
tc.assertAlmostEqual(get_avg_num_reviews([], "First Aid Beauty"), -1)