Introduction

I'm studying for the Royal Yachting Association's Day Skipper examination. I wrote the below script to help me practice converting magnetic bearings to true, and vice versa. It does not factor for compass deviation.

I thought I'd post this on the blog on the off chance this script may be useful to someone, somewhere, at sometime!

This script is also available at my GitHub account.

Code

import random
from random import randrange

c_direction = ["W", "E"]
h_type = ["M", "T"]

class Variation:
  def __init__(self, heading, c_direction):
    self.heading = heading
    self.c_direction = c_direction

class Heading:
  def __init__(self, heading, h_type):
    self.heading = heading
    self.h_type = h_type

class Response:
  def __init__(self, heading, h_type):
    self.heading = heading
    self.h_type = h_type

class Answer:
  def __init__(self, heading, h_type):
    self.heading = heading
    self.h_type = h_type

rounds_to_go = 0
rounds_done = 0

# Ask user how many rounds of practice
print("Hello! Welcome to this little app, where you can practice converting magnetic and true headings given datapoints regarding variation.")
rounds_to_go = input("How many times do you wish to practice? ")
while not rounds_to_go.isdigit():
  rounds_to_go = input("Enter a number only: ")

rounds_to_go = int(rounds_to_go)

# Loop
while rounds_to_go > rounds_done:

  variation = Variation(randrange(0, 30), random.choice(c_direction))
  heading = Heading(randrange(0, 359), random.choice(h_type))
  user_response = Response(0, h_type[0])
  correct_answer = Answer(0, h_type[0])
  result = 0

  print("\nConvert the following heading to magnetic or true, as applicable: " + str(heading.heading), str(heading.h_type))
  print("The variation for this problem is: " + str(variation.heading), str(variation.c_direction))

  user_response.heading = int(input("Please enter new compass heading (number only): "))
  user_response.h_type = str(input("Please indicate whether the heading is magnetic [M] or true [T]: "))

  print("You have entered: " + str(user_response.heading), str(user_response.h_type))

# if heading is true and variation is west
# answer heading is magnetic
# answer heading is equal to heading plus variation
# if this amount exceeds 359
# then subtract 359
# otherwise stet

  if heading.h_type == h_type[1] and variation.c_direction == c_direction[0]:
    correct_answer.h_type = h_type[0]
    result = heading.heading + variation.heading
    if result > 359:
      correct_answer.heading = result - 359
    else:
      correct_answer.heading = result

# if heading is true and variation is east
# answer heading is magnetic
# answer heading is equal to heading minus variation
# if this amount is less than 0
# then add 359
# otherwise stet

  if heading.h_type == h_type[1] and variation.c_direction == c_direction [1]:
    correct_answer.h_type = h_type[0]
    result = heading.heading - variation.heading
    if result < 0:
      correct_answer.heading = result + 359
    else:
      correct_answer.heading = result

# if heading is magnetic and variation is west
# answer heading is true
# answer heading is equal to heading minus variation
# if this amount is less than 0
# then add 359
# otherwise stet

  if heading.h_type == h_type[0] and variation.c_direction == c_direction[0]:
    correct_answer.h_type = h_type[1]
    result = heading.heading - variation.heading
    if result < 0:
      correct_answer.heading = result + 359
    else:
      correct_answer.heading = result

# if heading is magnetic and variation is east
# answer heading is true
# answer heading is equal to heading plus variation
# if this amount exceeds 359
# then substract 359
# otherwise stet

  if heading.h_type == h_type[0] and variation.c_direction == c_direction[1]:
    correct_answer.h_type = h_type[1]
    result = heading.heading + variation.heading
    if result > 359:
      correct_answer.heading = result - 359
    else:
      correct_answer.heading = result

# Print correct answer
  print("The correct answer is: " + str(correct_answer.heading), str(correct_answer.h_type))

# Check whether user's response is correct
  if user_response.h_type == correct_answer.h_type and user_response.heading == correct_answer.heading:
    print("You have entered the correct answer.")
  else:
    print("You have entered the incorrect answer.")

  rounds_done += 1

print("\nThanks for practicing with me!")
Hello! Welcome to this little app, where you can practice converting magnetic and true headings given datapoints regarding variation.
How many times do you wish to practice? 2

Convert the following heading to magnetic or true, as applicable: 92 M
The variation for this problem is: 2 W
Please enter new compass heading (number only): 90
Please indicate whether the heading is magnetic [M] or true [T]: T
You have entered: 90 T
The correct answer is: 90 T
You have entered the correct answer.

Convert the following heading to magnetic or true, as applicable: 241 T
The variation for this problem is: 7 E
Please enter new compass heading (number only): 234
Please indicate whether the heading is magnetic [M] or true [T]: M
You have entered: 234 M
The correct answer is: 234 M
You have entered the correct answer.

Thanks for practicing with me!