geekcomputers/Python · warning · ValueError

Degrees must be between 0 and 359

Error message

Degrees must be between 0 and 359

What it means

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.

Source

Thrown at convert_wind_direction_to_degrees.py:12

def degrees_to_compass(degrees):
    directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
    index = round(degrees / 45) % 8
    return directions[index]


# Taking input from the user
while True:
    try:
        degrees = float(input("Enter the wind direction in degrees (0-359): "))
        if degrees < 0 or degrees >= 360:
            raise ValueError("Degrees must be between 0 and 359")
        break
    except ValueError as ve:
        print(f"Error: {ve}")
        continue


compass_direction = degrees_to_compass(degrees)
print(f"{degrees} degrees is {compass_direction}")

View on GitHub (pinned to 40f4cd2652)

Solutions

  1. Enter a value from 0 to 359 inclusive; use 0 for north, not 360
  2. Normalize computed angles first: degrees % 360 before validation/input
  3. For non-numeric input, re-enter a plain number without units or symbols

Example fix

# before
Enter the wind direction in degrees (0-359): 360  # error

# after
degrees = degrees % 360
# then enter/submit e.g. 0
Defensive patterns

Strategy: validation

Validate before calling

degrees = float(input('Enter the wind direction in degrees (0-359): '))
degrees = degrees % 360
if 0.0 <= degrees < 360.0:
    ...  # proceed

Type guard

def is_valid_degree(d) -> bool:
    return isinstance(d, (int, float)) and 0.0 <= d < 360.0

Try / catch

while True:
    try:
        degrees = float(input('Degrees (0-359): '))
        if not (0.0 <= degrees < 360.0):
            raise ValueError('Degrees must be between 0 and 359')
        break
    except ValueError as e:
        print(f'Error: {e}')

Prevention

When it happens

Trigger: 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'.

Common situations: 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.

Related errors


AI-assisted analysis of geekcomputers/Python@40f4cd2652 (2026-08-27). Data as JSON: /api/errors/e7a7b00921ca8356. Report an issue: GitHub.