Solved problem9 in python3