Solving problem 6 using python