I'm sure that nobody here is willing to write it for you. However, I believe that jkn was right in trying to get you to solve the problem. ;)
You know what you need to do, but how are you going to do it? Create a flow chart ;) -- http://mail.python.org/mailman/listinfo/python-list