Update 4.py

Made it Python 2.7 compatible, and fixed some bugs.