The input on portb.2 was left floating which means that it's not really ground nor is it vcc. A floating input can change it state be simpling touch it or even something around it. Think of it this way, if the input requires say 0.7V to register as a low and 1.2V to register as a high you now have a 500mV dead zone than can go either way, pull in one way and the problem is resolved because you now know where it will always be without any other for of influence like a switch.