It’s more straightforward than you think:
def getCardinalDirection(angle):
directions = ['↑ North', '↗ North East', '→ East', '↘ South East', '↓ South', '↙ South West', '← West', '↖ North West']
return directions[round(angle / 45) % 8] # round() is a built-in function
Example output:
>>> getCardinalDirection(50)
'↗ North East'
>>> getCardinalDirection(220)
'↙ South West'
>>> getCardinalDirection(-37)
'↖ North West'
>>> getCardinalDirection(188)
'↓ South'
solved convert js function to Python [closed]