{"record":{"id":"e7ea4b3e7ae28a93","repo":"TheAlgorithms/Python","slug":"height-of-grid-can-t-be-0-or-negative","errorCode":null,"errorMessage":"Height of grid can't be 0 or negative","messagePattern":"Height of grid can't be 0 or negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ciphers/rail_fence_cipher.py","lineNumber":27,"sourceCode":"\r\n    >>> encrypt(\"Hello World\", 4)\r\n    'HWe olordll'\r\n\r\n    >>> encrypt(\"This is a message\", 0)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Height of grid can't be 0 or negative\r\n\r\n    >>> encrypt(b\"This is a byte string\", 5)\r\n    Traceback (most recent call last):\r\n        ...\r\n    TypeError: sequence item 0: expected str instance, int found\r\n    \"\"\"\r\n    temp_grid: list[list[str]] = [[] for _ in range(key)]\r\n    lowest = key - 1\r\n\r\n    if key <= 0:\r\n        raise ValueError(\"Height of grid can't be 0 or negative\")\r\n    if key == 1 or len(input_string) <= key:\r\n        return input_string\r\n\r\n    for position, character in enumerate(input_string):\r\n        num = position % (lowest * 2)  # puts it in bounds\r\n        num = min(num, lowest * 2 - num)  # creates zigzag pattern\r\n        temp_grid[num].append(character)\r\n    grid = [\"\".join(row) for row in temp_grid]\r\n    output_string = \"\".join(grid)\r\n\r\n    return output_string\r\n\r\n\r\ndef decrypt(input_string: str, key: int) -> str:\r\n    \"\"\"\r\n    Generates a template based on the key and fills it in with\r\n    the characters of the input string and then reading it in\r\n    a zigzag formation.\r","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/rail_fence_cipher.py#L9-L45","documentation":"Raised by encrypt() in ciphers/rail_fence_cipher.py when the rail count (key) is <= 0. A rail fence zigzag needs at least one rail, so 0 or negative heights are rejected with a ValueError. Note key==1 or len(input) <= key short-circuits to returning the input unchanged.","triggerScenarios":"encrypt('message', 0); encrypt('message', -10); key derived from len arithmetic that underflowed to 0; passing a bool False (== 0).","commonSituations":"User-supplied key parsed with int() that accepted 0 or a negative number; key = rails - 2 style off-by-one math; config defaults left unset.","solutions":["Pass a positive rail count: encrypt('message', 2) or higher","Validate at the UI boundary: reject keys < 1 before calling","Remember key=1 returns the plaintext unchanged (no zigzag)"],"exampleFix":"# before\nencrypt('message', 0)\n\n# after\nencrypt('message', 3)","handlingStrategy":"validation","validationCode":"if not isinstance(key, int) or key < 1:\n    raise ValueError('rail count must be a positive integer')","typeGuard":"def valid_rail_key(key) -> bool:\n    return isinstance(key, int) and key >= 1","tryCatchPattern":"try:\n    ciphertext = encrypt(message, key)\nexcept ValueError as exc:\n    if \"Height of grid\" in str(exc):\n        raise ConfigError('rail fence key must be >= 1') from exc\n    raise","preventionTips":["Reject keys < 1 at your input boundary","Remember key=1 returns plaintext unchanged (no encryption)","Validate the key the same way on encrypt and decrypt sides"],"tags":["rail-fence","transposition","key-validation","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}