working problem 5 in python