Skip to content

Commit 7c23bdf

Browse files
authored
Updated tests error messages and reformatted hints. (#3539)
[no important files changed]
1 parent 4c7e68c commit 7c23bdf

File tree

3 files changed

+100
-41
lines changed

3 files changed

+100
-41
lines changed

exercises/concept/inventory-management/.docs/hints.md

+28-15
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,48 @@
22

33
## General
44

5-
- [The Python Dictionary Tutorial](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) can be a great introduction.
5+
- [The Python Dictionary Tutorial][dict-tutorial] can be a great place to start.
6+
- The Python docs on [Mapping Types - dicts][dict docs] is also pretty helpful.
67

78
## 1. Create an inventory based on a list
89

9-
- You need a [for loop](https://docs.python.org/3/tutorial/controlflow.html#for-statements) to iterate the list of items, then insert each item in the dictionary if missing and increment the item count using the dictionary accessor.
10-
- You can use [`setdefault`](https://www.w3schools.com/python/ref_dictionary_setdefault.asp) to make sure the value is set before incrementing the count of the item.
11-
- This function should [return a dict](https://www.w3schools.com/python/ref_keyword_return.asp).
10+
- You need a [for loop][for-loop] to iterate the list of items, then insert each item in the dictionary if missing and increment the item count using the dictionary accessor.
11+
- You can use [`dict.setdefault`][dict setdefault] to make sure the value is set before incrementing the count of the item.
12+
- This function should [return][return-keyword] a dict].
1213

1314
## 2. Add items from a list to an existing dictionary
1415

15-
- You need a [for loop](https://docs.python.org/3/tutorial/controlflow.html#for-statements) to iterate the list of items, then insert each item if not already in the dictionary and [increment](https://www.w3schools.com/python/gloss_python_assignment_operators.asp) the item count using the dictionary accessor.
16-
- You can use [`setdefault`](https://www.w3schools.com/python/ref_dictionary_setdefault.asp) to make sure the value is set before incrementing the count of the item.
16+
- You need a [for loop][for-loop] to iterate the list of items, then insert each item if not already in the dictionary and [increment][increment] the item count using the dictionary accessor.
17+
- You can use [`dict.setdefault`][dict setdefault] to make sure the value is set before incrementing the count of the item.
1718
- The function `add_items` can be used by the `create_inventory` function with an empty dictionary in parameter.
18-
- This function should [return a dict](https://www.w3schools.com/python/ref_keyword_return.asp).
19+
- This function should [return][return-keyword] a dict.
1920

2021
## 3. Decrement items from the inventory
2122

22-
- You need [for loop](https://docs.python.org/3/tutorial/controlflow.html#for-statements) to iterate the list of items, if the number of items is not `0` then [decrement](https://www.w3schools.com/python/gloss_python_assignment_operators.asp) the current number of items.
23-
- You can use the `key in dict` that returns `True` if the key exists to make sure the value is in the dictionary before decrementing the number of items.
24-
- This function should [return a dict](https://www.w3schools.com/python/ref_keyword_return.asp).
23+
- You need [for loop][for-loop] to iterate the list of items, if the number of items is not `0` then [decrement][decrement] the current number of items.
24+
- You can use the check `key in dict` that returns `True` if the key exists to make sure the value is in the dictionary before decrementing the number of items.
25+
- This function should [return][return-keyword] a dict.
2526

2627
## 4. Remove an item entirely from the inventory
2728

28-
- If item is in the dictionary, [remove it](https://www.w3schools.com/python/ref_dictionary_pop.asp).
29+
- If item is in the dictionary, [remove it][dict-pop].
2930
- If item is not in the dictionary, do nothing.
30-
- This function should [return a dict](https://www.w3schools.com/python/ref_keyword_return.asp).
31+
- This function should [return][return-keyword] a dict.
3132

3233
## 5. Return the inventory content
3334

34-
- You need [for loop](https://docs.python.org/3/tutorial/controlflow.html#for-statements) on the inventory and if the number of item is greater of `0` then append the tuple to a list.
35-
- You can use `dict.items()` to iterate on both the item and the value at the same time, `items()` returns a tuple that you can use as it is or deconstruct.
36-
- This function should [return](https://www.w3schools.com/python/ref_keyword_return.asp) a [list](https://docs.python.org/3/tutorial/introduction.html#lists) of [tuples](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences).
35+
- You need to use a [for loop][for-loop] on the inventory and if the number of item is greater of `0` then append the `tuple` to a `list`.
36+
- You can use [`dict.items()`][dict items] to iterate on both the item and the value at the same time, `items()` returns a `tuple` that you can use or deconstruct, if needed.
37+
- This function should [return][return-keyword] a [list][list] of [tuples][tuples].
38+
39+
[decrement]: https://www.w3schools.com/python/gloss_python_assignment_operators.asp
40+
[dict docs]: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
41+
[dict items]: https://docs.python.org/3/library/stdtypes.html#dict.items
42+
[dict setdefault]: https://www.w3schools.com/python/ref_dictionary_setdefault.asp
43+
[dict-pop]: https://www.w3schools.com/python/ref_dictionary_pop.asp
44+
[dict-tutorial]: https://docs.python.org/3/tutorial/datastructures.html#dictionaries
45+
[for-loop]: https://docs.python.org/3/tutorial/controlflow.html#for-statements
46+
[increment]: https://www.w3schools.com/python/gloss_python_assignment_operators.asp
47+
[list]: https://docs.python.org/3/tutorial/introduction.html#lists
48+
[return-keyword]: https://www.w3schools.com/python/ref_keyword_return.asp
49+
[tuples]: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

exercises/concept/inventory-management/dicts.py

+1
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ def list_inventory(inventory):
5252
"""
5353

5454
pass
55+
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,107 @@
11
import unittest
22
import pytest
3-
from dicts import create_inventory, add_items, decrement_items, remove_item, list_inventory
3+
from dicts import (create_inventory,
4+
add_items,
5+
decrement_items,
6+
remove_item,
7+
list_inventory)
48

59

610
class InventoryTest(unittest.TestCase):
711

812
@pytest.mark.task(taskno=1)
913
def test_create_inventory(self):
10-
self.assertEqual(create_inventory(["wood", "iron", "iron", "diamond", "diamond"]),
11-
{"wood": 1, "iron": 2, "diamond": 2})
14+
15+
actual_result = create_inventory(["wood", "iron", "iron", "diamond", "diamond"])
16+
expected = {"wood": 1, "iron": 2, "diamond": 2}
17+
error_message = ('Called create_inventory(["wood", "iron", "iron", "diamond", "diamond"]). '
18+
f'The function returned {actual_result}, but the tests expected {expected}.')
19+
20+
self.assertEqual(actual_result, expected, msg=error_message)
1221

1322
@pytest.mark.task(taskno=2)
1423
def test_add_one_item(self):
15-
self.assertEqual(add_items({"wood": 4, "iron": 2}, ["iron", "iron"]),
16-
{"wood": 4, "iron": 4})
24+
actual_result = add_items({"wood": 4, "iron": 2}, ["iron", "iron"])
25+
expected = {"wood": 4, "iron": 4}
26+
error_message = ('Called add_items({"wood": 4, "iron": 2}, ["iron", "iron"]). '
27+
f'The function returned {actual_result}, but the tests expected {expected}.')
28+
29+
self.assertEqual(actual_result, expected, msg=error_message)
1730

1831
@pytest.mark.task(taskno=2)
1932
def test_add_multiple_items(self):
20-
self.assertEqual(add_items({"wood": 2, "gold": 1, "diamond": 3}, ["wood", "gold", "gold"]),
21-
{"wood": 3, "gold": 3, "diamond": 3})
33+
actual_result = add_items({"wood": 2, "gold": 1, "diamond": 3}, ["wood", "gold", "gold"])
34+
expected = {"wood": 3, "gold": 3, "diamond": 3}
35+
error_message = ('Called add_items({"wood": 2, "gold": 1, "diamond": 3}, ["wood", "gold", "gold"]). '
36+
f'The function returned {actual_result}, but the tests expected {expected}.')
37+
38+
self.assertEqual(actual_result, expected, msg=error_message)
2239

2340
@pytest.mark.task(taskno=2)
2441
def test_add_new_item(self):
25-
self.assertEqual(add_items({"iron": 1, "diamond": 2}, ["iron", "wood", "wood"]),
26-
{"iron": 2, "diamond": 2, "wood": 2})
42+
actual_result = add_items({"iron": 1, "diamond": 2}, ["iron", "wood", "wood"])
43+
expected = {"iron": 2, "diamond": 2, "wood": 2}
44+
error_message = ('Called add_items({"iron": 1, "diamond": 2}, ["iron", "wood", "wood"]). '
45+
f'The function returned {actual_result}, but the tests expected {expected}.')
46+
47+
self.assertEqual(actual_result, expected, msg=error_message)
2748

2849
@pytest.mark.task(taskno=2)
2950
def test_add_from_empty_dict(self):
30-
self.assertEqual(add_items({}, ["iron", "iron", "diamond"]),
31-
{"iron": 2, "diamond": 1})
51+
actual_result = add_items({}, ["iron", "iron", "diamond"])
52+
expected = {"iron": 2, "diamond": 1}
53+
error_message = ('Called add_items({}, ["iron", "iron", "diamond"]). '
54+
f'The function returned {actual_result}, but the tests expected {expected}.')
55+
56+
self.assertEqual(actual_result, expected, msg=error_message)
3257

3358
@pytest.mark.task(taskno=3)
3459
def test_decrement_items(self):
35-
self.assertEqual(decrement_items({"iron": 3, "diamond": 4, "gold": 2},
36-
["iron", "iron", "diamond", "gold", "gold"]),
37-
{"iron": 1, "diamond": 3, "gold": 0})
60+
actual_result = decrement_items({"iron": 3, "diamond": 4, "gold": 2},
61+
["iron", "iron", "diamond", "gold", "gold"])
62+
expected = {"iron": 1, "diamond": 3, "gold": 0}
63+
error_message = ('Called decrement_items({"iron": 3, "diamond": 4, "gold": 2},'
64+
'["iron", "iron", "diamond", "gold", "gold"]). The function '
65+
f'returned {actual_result}, but the tests expected {expected}.')
66+
67+
self.assertEqual(actual_result, expected, msg=error_message)
3868

3969
@pytest.mark.task(taskno=3)
4070
def test_not_below_zero(self):
41-
self.assertEqual(decrement_items({"wood": 2, "iron": 3, "diamond": 1},
42-
["wood", "wood", "wood", "iron", "diamond", "diamond"]),
43-
{"wood": 0, "iron": 2, "diamond": 0})
71+
actual_result = decrement_items({"wood": 2, "iron": 3, "diamond": 1},
72+
["wood", "wood", "wood", "iron", "diamond", "diamond"])
73+
expected = {"wood": 0, "iron": 2, "diamond": 0}
74+
error_message = ('Called decrement_items({"wood": 2, "iron": 3, "diamond": 1}, '
75+
'["wood", "wood", "wood", "iron", "diamond", "diamond"]). The '
76+
f'function returned {actual_result}, but the tests expected {expected}.')
77+
78+
self.assertEqual(actual_result, expected, msg=error_message)
4479

4580
@pytest.mark.task(taskno=4)
4681
def test_remove_item(self):
47-
self.assertEqual(remove_item({"iron": 1, "diamond": 2, "gold": 1}, "diamond"),
48-
{"iron": 1, "gold": 1})
82+
actual_result = remove_item({"iron": 1, "diamond": 2, "gold": 1}, "diamond")
83+
expected = {"iron": 1, "gold": 1}
84+
error_message = ('Called remove_item({"iron": 1, "diamond": 2, "gold": 1}, "diamond"). '
85+
f'The function returned {actual_result}, but the tests expected {expected}.')
86+
87+
self.assertEqual(actual_result, expected, msg=error_message)
4988

5089
@pytest.mark.task(taskno=4)
5190
def test_remove_item_not_in_inventory(self):
52-
self.assertEqual(remove_item({"iron": 1, "diamond": 2, "gold": 1}, "wood"),
53-
{"iron": 1, "gold": 1, "diamond": 2})
91+
actual_result = remove_item({"iron": 1, "diamond": 2, "gold": 1}, "wood")
92+
expected = {"iron": 1, "gold": 1, "diamond": 2}
93+
error_message = ('Called remove_item({"iron": 1, "diamond": 2, "gold": 1}, "wood"). '
94+
f'The function returned {actual_result}, '
95+
f'but the tests expected {expected}.')
96+
97+
self.assertEqual(actual_result, expected, msg=error_message)
5498

5599
@pytest.mark.task(taskno=5)
56100
def test_list_inventory(self):
57-
self.assertEqual(list_inventory({"coal": 15, "diamond": 3, "wood": 67, "silver": 0}),
58-
[("coal", 15), ("diamond", 3), ("wood", 67)])
59-
101+
actual_result = list_inventory({"coal": 15, "diamond": 3, "wood": 67, "silver": 0})
102+
expected = [("coal", 15), ("diamond", 3), ("wood", 67)]
103+
error_message = ('Called list_inventory({"coal": 15, "diamond": 3, "wood": 67, "silver": 0}). '
104+
f'The function returned {actual_result}, '
105+
f'but the tests expected {expected}.')
60106

61-
if __name__ == "__main__":
62-
unittest.main()
107+
self.assertEqual(actual_result, expected, msg=error_message)

0 commit comments

Comments
 (0)