Nowhere inside your while loop do you change BER. That means if the while condition is satisfied initially, it will always be satisfied and so your while loop will never end. You need to change BER inside your loop.
While P_avg depends upon SNR which is computed inside the loop, SNR only depends on values that don't change inside the loop so P_avg doesn't actually change. So defining BER in terms of SNR and P_avg won't work to change it for purposes of the while condition.
As an analogy, if you said "While I'm not home, I'm going to walk forward one step from work" you'll never get home (unless you work at home.) If you said "While I'm not home, I'm going to walk forward one step from where I am now" you will probably eventually get home (assuming you're facing in the right direction.)
