Some Hard Problem Using For Loops In Python