Solvd problem8 in python3