When navigating at sea, sailors need to know where it is safe to travel and where underwater dangers such as rocks, reefs or wrecks may be hiding beneath the surface. Not every hazard can be seen from a boat, so navigation buoys are used to mark hazards and guide vessels safely around them.
In this Python challenge, you will investigate a particular type of navigation marker: the cardinal buoy. You will then use selection (if statements) and coordinates to write a program that automatically chooses the correct cardinal buoy based on its position relative to an underwater danger.
What are Cardinal Buoys?

Cardinal buoys are used to indicate where the safe water lies in relation to a hazard. They are named after the four cardinal points of a compass: North – East – South – West
The name of the buoy tells a sailor which side of the buoy they should pass. For example, a North Cardinal Buoy tells you that the safest/deepest water is to the north of the buoy, while a West Cardinal Buoy indicates that you should pass to its west.

Cardinal buoys are easy to recognise because they use distinctive black and yellow markings, together with two black triangles pointing either up ▲ or down ▼.
- North: black above yellow, with both triangles pointing upwards.
- East: black-yellow-black, with the triangles pointing away from each other.
- South: yellow above black, with both triangles pointing downwards.
- West: yellow-black-yellow, with the triangles pointing towards each other.
These markings allow sailors to quickly identify the buoy and determine where the safe water is located.
The Coding Challenge
For this challenge, an underwater rock is positioned in the centre of the screen. Your program will randomly generate a position (x, y) for a new buoy somewhere around this danger.
Your task is to determine which of the four cardinal buoys should be placed at that position.
Some of the Python code has already been provided for you. This includes the code required to draw the sea and underwater danger, as well as a subroutine called:
addBuoy(cardinalPoint, x, y)
The subroutine requires three parameters:
- The type of cardinal buoy: “North”, “South”, “East” or “West”
- Its x-coordinate
- Its y-coordinate
For example, the following code will draw a West Cardinal Buoy at coordinates (-100, 20):
addBuoy("West", -100, 20)
Note that the centre of the screen represents the danger and has coordinates (0, 0).
Your task consist of using the Python IDE below to complete the code needed to randomly generate a set of (x,y) coordinates (each between -150 and 150).
import random x = random.randint(-150,150) y = random.randint(-150,150)
Then you will need to implement the following flowchart using Python code to let the computer decide which cardinal point to use based on its randomly generated position.

Note that you will need to use the abs() function in python to get the absolute value.
e.g.
if x<0 and abs(x) >= abs(y):
addBuoy("West", -100, 20)
Python Code:
Compete the code below:

Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area






