Autumn Maker School: Hexagon Program

This post is part of a larger series called the Autumn Maker SchoolThe idea is to make ten things, mostly useful or to self-teach certain skills, and then write about them.  The first post was about a graphic-design project, the Astrological Volvelle. This is the second project.

I am not always very smart when it comes to teaching programming.  I’m going very slowly, and it’s challenging both to me and to the students.  I keep throwing a program up on the projector screen for them to look at, and then I discover that it’s wrong. Argh. It worked fine when I made it the other night, why isn’t it working now?

Mostly it’s that I barely understand what I’m doing, yet.  In three years, I’m going to be amazing.  But first I have to live through these three years of agony.

Here’s the code I wrote tonight.  It is not elegant.

# Hexagon Calculator
# by Andrew B. Watt

#imports arguments from system library
from sys import argv
# turns three arguments into the script name, the side, and the units
script, side, units = argv
# turns the side value into a number from a text string, makes it a variable.
side1 = int(side)

# Reports the values of the three given arguments
print "This script calculates values for hexagons using", script
print "The units are called:", units
print "The side length is:", side

# Brings in the math system more completely
import math
# calculates the perimeter of the hexagon.
perimeter = side1 * 6
# Stores the square root value of 3 to use in the area equation
sqRt = math.sqrt(3)
# calculates the area of the hexagon.
area = 3 * sqRt * (side1 * side1)/2
print "The area of the hexagon is %r square %s." % (area, units)
print "The perimeter of the hexagon is %r %s." % (perimeter, units)

The purpose of the code is to calculate the area and perimeter of a hexagon. Happily, I can report that this code worked successfully.  But it took me a long time to get it right, and ten run-throughs, before it operated properly.

Here’s what it does:  it calculates the perimeter and the area of a hexagon, and feeds it back to you using the unit type you designated at the beginning of the operation.  Including my comments, it takes up twenty-five lines of code.  Not including my comments… It’s twelve lines of code.  I could probably make it 8 lines of code, but I was also learning how to use the import math function, and the argv function.

But better — I now understand how argv works in the python computer language, and I understand how I can access higher mathematical functions in python.  It took me a long time to understand that the name of the file was itself an argument in the command. Now I understand how to use additional variables in order to store a broader range of information, and how to use argv to access the name of the file itself when I write a program.  What I’m going to use that for, or how I’m going to teach the use of that to my students, I don’t know.

Their own efforts are coming along nicely.  We’ve written a short script that generates the caesar cipher codes for their email messages.  It’s not a secure code at all, but it gets them thinking about security and about the difference between secure and insecure channels — which is no bad thing, to my mind.  They wrote short computer programs which calculate the area of a triangle, the area of a circle, and the area of a square or rectangle — and then they built that code into a type of meta-code, which asks the user what sort of polygon they want to solve for, and then uses the data they give it to calculate area, perimeter/circumference, and so on.  We got into if-elif-else constructions on that one.  Now I can have them write a program where they tell the computer what sort of polygons they’re working with, right at the command line, using argv.

But again, I say, I am not fast at learning programming, nor at teaching it.  I’m learning these little bits and pieces, and gradually learning how to string them together to do things.  I’m not yet clear how how being able to tell a computer to do these things, in the right sequences, makes computers do cool things.

Still, I feel that I’m learning the basics.

Liked it? Take a second to support Andrew on Patreon!
Become a patron at Patreon!

12 comments

Leave a Reply to Autumn Maker School: Braiding Disk | Wanderings in the LabyrinthCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.