{"record":{"id":"33998cd470493485","repo":"TheAlgorithms/Python","slug":"hand-should-contain-only-5-cards-hand-r","errorCode":null,"errorMessage":"Hand should contain only 5 cards: {hand!r}","messagePattern":"Hand should contain only 5 cards: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"project_euler/problem_054/sol1.py","lineNumber":128,"sourceCode":"\n        The cards should be of the following format:\n        [card value][card suit]\n\n        The first character is the value of the card:\n        2, 3, 4, 5, 6, 7, 8, 9, T(en), J(ack), Q(ueen), K(ing), A(ce)\n\n        The second character represents the suit:\n        S(pades), H(earts), D(iamonds), C(lubs)\n\n        For example: \"6S 4C KC AS TH\"\n        \"\"\"\n        if not isinstance(hand, str):\n            msg = f\"Hand should be of type 'str': {hand!r}\"\n            raise TypeError(msg)\n        # split removes duplicate whitespaces so no need of strip\n        if len(hand.split(\" \")) != 5:\n            msg = f\"Hand should contain only 5 cards: {hand!r}\"\n            raise ValueError(msg)\n        self._hand = hand\n        self._first_pair = 0\n        self._second_pair = 0\n        self._card_values, self._card_suit = self._internal_state()\n        self._hand_type = self._get_hand_type()\n        self._high_card = self._card_values[0]\n\n    @property\n    def hand(self):\n        \"\"\"Returns the self hand\"\"\"\n        return self._hand\n\n    def compare_with(self, other: PokerHand) -> str:\n        \"\"\"\n        Determines the outcome of comparing self hand with other hand.\n        Returns the output as 'Win', 'Loss', 'Tie' according to the rules of\n        Texas Hold'em.\n","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_054/sol1.py#L110-L146","documentation":"Raised by the PokerHand constructor in project_euler/problem_054/sol1.py when hand.split(' ') does not yield exactly 5 tokens. The parser expects exactly five two-character cards separated by single spaces; 4 or 6 cards, trailing spaces, or tab/multiple-space separators all change the token count and trigger this ValueError.","triggerScenarios":"PokerHand('6S 4C KC AS') (4 cards), PokerHand('6S 4C KC AS TH 2D') (6 cards), PokerHand('6S 4C KC AS TH ') (trailing space -> 6 tokens), or tab-separated input like '6S\\t4C\\tKC\\tAS\\tTH' (split on ' ' yields 1 token).","commonSituations":"Dirty data files with trailing whitespace or double spaces; lines from the classic p054_poker.txt split incorrectly; CSV rows where a field was dropped or extra column added.","solutions":["Normalize whitespace before constructing: PokerHand(' '.join(hand.split())).","Validate card count upstream: assert len(hand.split()) == 5.","Fix the data source (strip trailing newlines/spaces, correct column counts).","Log the offending line to find malformed rows in batch processing."],"exampleFix":"# before\nraw = '6S 4C KC AS TH '\\n p = PokerHand(raw)  # ValueError: 6 tokens from trailing space\n\n# after\np = PokerHand(' '.join(raw.split()))","handlingStrategy":"validation","validationCode":"tokens = ' '.join(raw_line.split()).split(' ')\nif len(tokens) != 5:\n    raise ValueError(f\"expected 5 cards, got {len(tokens)}: {raw_line!r}\")\nhand = PokerHand(' '.join(tokens))","typeGuard":"def is_five_card_hand(line: str) -> bool:\n    return isinstance(line, str) and len(line.split()) == 5","tryCatchPattern":"try:\n    hand = PokerHand(line)\nexcept ValueError as e:\n    logger.warning(\"skipping malformed line %r: %s\", line, e)\n    continue","preventionTips":["Normalize whitespace with ' '.join(line.split()) before parsing.","Strip trailing newlines when reading files line by line.","Validate 5 tokens per row when loading batch data; skip-and-log bad rows."],"tags":["project-euler","poker","validation","valueerror","data-format"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}