What about setting the
Terminal -> Preferences -> Settings -> Shell -> When the shell exits: Close the window
Now just make sure that the last thing your script does is exit from the shell and your window will be closed.
Then again, do you "Really" need a Terminal session to run your code?
That is to say, running programs and scripts can be done without needing a terminal session. The Terminal session is just a window to the command line, and you do not need to display anything via the Terminal display, then there is no reason to have it open to run your program.
You can use Applescript ("do shell script" statement), or Automator ("run shell script" action), or you can use a 3rd party utility such as Platypus that will put your program or script in a double clickable app that does not need a Terminal session.
Another option is to use a different Terminal emulator, such as iTerm which also has the ability to close the window when the shell exits. It can even automatically quit iterm when the last window is closed.