200-901 · Question #415
Drag and Drop Question Drag and drop the code from the bottom onto the box where the code is missing to complete the error path scenario. Not all options are used. Answer:
The correct answer is application/xml; iana-if-type:ethernetCsmacd; iana-if-type:ethernetCsmacd; iana-if-type:softwareLoopback. This drag-and-drop question tests Python class design, pytest unit testing for error paths, and conditional logic by requiring the test-taker to complete a Player class and its associated test suite.
Question
Drag and Drop Question Drag and drop the code from the bottom onto the box where the code is missing to complete the error path scenario. Not all options are used. Answer:
Exhibit
Answer Area
Drag items
Correct arrangement
- application/xml
- iana-if-type:ethernetCsmacd
- iana-if-type:ethernetCsmacd
- iana-if-type:softwareLoopback
Explanation
This drag-and-drop question tests Python class design, pytest unit testing for error paths, and conditional logic by requiring the test-taker to complete a Player class and its associated test suite.
Approach. 1. Blank in Player.__init__(self, name): The __init__ method should initialize all instance attributes. Since rating is not passed as an argument to __init__, it should be initialized to a default value. Drag self.rating = None to this blank. This is a common practice for attributes that may not have a value immediately.
2. Blank in set_rating(self, rating) (the primary if condition leading to raise ValueError): The provided pytest tests (test_add_higher_rating with 101 and test_add_lower_rating with -1) indicate that ratings outside the 1-100 range are invalid. Drag if rating < 1 or rating > 100: to this blank. This condition correctly identifies ratings that should raise a ValueError. The second image explicitly shows this as the correct fill.
3. Blank in set_rating(self, rating) (within the else block after the error condition): This else block is executed when rating is valid (between 1 and 100, inclusive). The second image explicitly shows if rating != 0: as the correct fill for this blank. Although this condition is redundant if the previous check rating < 1 has passed, and the method does not actually set self.rating, we must follow the explicit solution shown in the 'Answer Area' image.
4. Blank in test_add_invalid_rating (player.set_rating(...)): This test expects a ValueError to be raised. Given the if rating < 1 or rating > 100: validation, 0 is an invalid rating (0 < 1). Drag player.set_rating(0) to this blank. This will trigger the ValueError as expected by pytest.raises(). The second image confirms this placement.
Common mistakes.
- common_mistake. A common mistake is selecting
player.set_rating(85)for thetest_add_invalid_ratingblank.85is a valid rating according to the1 <= rating <= 100range, and therefore, it would not raise aValueError. This would cause thepytest.raises(ValueError)context manager to fail the test. Another potential mistake is attempting to placeself.rating = ratingin theelseblock ofset_ratingwhereif rating != 0:is shown in the solution. Whileself.rating = ratingwould be more functionally appropriate for aset_ratingmethod, the provided 'Answer Area' in the second image clearly indicatesif rating != 0:as the intended solution for that specific blank in this problem's context. Misinterpreting theif/elsestructure or ignoring the explicit fills in the 'Answer Area' image would also lead to incorrect choices.
Concept tested. Python class definition, instance attribute initialization, method implementation, conditional logic (if/else), exception handling (raise ValueError), and unit testing with pytest for expected exceptions (pytest.raises).
Topics
Community Discussion
No community discussion yet for this question.
