Updated code for Python 3 with backward compatibility

Updated for Python 3 which is also compatible for Python 2.
Also verifying input from user