How do I improve this code? (assignment tagged below)

This is essentially a duplicate of Why won't this code run?.