rewrite chapter3 for python3