{"record":{"id":"e7a7b00921ca8356","repo":"geekcomputers/Python","slug":"degrees-must-be-between-0-and-359","errorCode":null,"errorMessage":"Degrees must be between 0 and 359","messagePattern":"Degrees must be between 0 and 359","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"convert_wind_direction_to_degrees.py","lineNumber":12,"sourceCode":"def degrees_to_compass(degrees):\n    directions = [\"N\", \"NE\", \"E\", \"SE\", \"S\", \"SW\", \"W\", \"NW\"]\n    index = round(degrees / 45) % 8\n    return directions[index]\n\n\n# Taking input from the user\nwhile True:\n    try:\n        degrees = float(input(\"Enter the wind direction in degrees (0-359): \"))\n        if degrees < 0 or degrees >= 360:\n            raise ValueError(\"Degrees must be between 0 and 359\")\n        break\n    except ValueError as ve:\n        print(f\"Error: {ve}\")\n        continue\n\n\ncompass_direction = degrees_to_compass(degrees)\nprint(f\"{degrees} degrees is {compass_direction}\")\n","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/geekcomputers/Python/blob/40f4cd2652d75ef8e49d76e5c4d431d458712719/convert_wind_direction_to_degrees.py#L1-L21","documentation":"This ValueError is raised by this interactive wind-direction conversion script when the user-entered degree value is negative or >= 360. Meteorological compass degrees are defined on [0, 360), so values like -5 or 360 fall outside the representable range; the loop catches the ValueError, prints 'Error: ...', and re-prompts until valid input is given.","triggerScenarios":"Running the script and entering 360, 370, -10, or 720 at the input prompt. Note that non-numeric input also raises ValueError from float() and is caught by the same handler with the message 'could not convert string to float'.","commonSituations":"Users entering 360 for north (valid is 0), negative degrees from calculations, or wrapped/accumulated angles from upstream math that were not normalized with % 360.","solutions":["Enter a value from 0 to 359 inclusive; use 0 for north, not 360","Normalize computed angles first: degrees % 360 before validation/input","For non-numeric input, re-enter a plain number without units or symbols"],"exampleFix":"# before\nEnter the wind direction in degrees (0-359): 360  # error\n\n# after\ndegrees = degrees % 360\n# then enter/submit e.g. 0","handlingStrategy":"validation","validationCode":"degrees = float(input('Enter the wind direction in degrees (0-359): '))\ndegrees = degrees % 360\nif 0.0 <= degrees < 360.0:\n    ...  # proceed","typeGuard":"def is_valid_degree(d) -> bool:\n    return isinstance(d, (int, float)) and 0.0 <= d < 360.0","tryCatchPattern":"while True:\n    try:\n        degrees = float(input('Degrees (0-359): '))\n        if not (0.0 <= degrees < 360.0):\n            raise ValueError('Degrees must be between 0 and 359')\n        break\n    except ValueError as e:\n        print(f'Error: {e}')","preventionTips":["Use 0 for north instead of 360","Normalize computed angles with % 360 before validating","Strip units/symbols from input before float() conversion"],"tags":["python","cli","input-validation","angle-range"],"backgroundTag":"input-out-of-range","analyzedSha":"40f4cd2652d75ef8e49d76e5c4d431d458712719","analyzedAt":"2026-08-27T11:12:20.313Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}