QuestionQ552

Software Development and Design

Complete the error-path scenario by dragging code from the bottom into the missing code boxes. Not all options are used.

Drag & Drop
player.set_rating(0)
player.set_rating(85)
if rating < 1 or rating > 100:
self.rating = None
if rating != 0:
if rating or rating is int(0):
self.rating = rating
import pytest
class Player:
    def __init__(self, name):
        self.name = name
        

    def set_rating(self, rating):
        if rating or rating is int(0):
            
                raise ValueError('rating is invalid', rating)
            else:
                
                return True
        else:
            return False

@pytest.fixture
def player():
    player = Player('User One')
    yield player
    del player

@pytest.mark.usefixtures('player')
class TestErrorPathSetRating:
    def test_add_higher_rating(self, player):
        with pytest.raises(ValueError):
            player.set_rating(101)

    def test_add_invalid_rating(self, player):
        with pytest.raises(ValueError):
            

    def test_add_lower_rating(self, player):
        with pytest.raises(ValueError):
            player.set_rating(-1)
Explanation

The accepted rating range is 1–100 inclusive. Values above 100, below 1, and zero are invalid and raise ValueError. A valid rating is assigned to self.rating, while the constructor initializes that attribute to None.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!