def of 'balanced' is not correctly defined in the while loop