You are redefining n before and inside the while loop, but you might only need to define it inside the while loop.