次 is more widely used than 遍, so if you are not sure for now, just use 次, people will understand you. and in many collocations, you should not replace 次 by 遍.
but to native speakers, there is a subtle difference between 次and遍, and usually they can tell the nuance of them.
遍 emphasizes the whole process of doing something.
the main image comes along with 遍 is that a certain movement is continuous from the beginning to the end. so in many cases it can help you to express your opinions or emotions using 遍.
the hidden meaning behind 这部电影我看了三遍 is one watched a movie three times and every time from the beginning to the end, but 看了三次 does not emphasize the watching process at all.
now can you tell the nuance between 写了三遍 and 写了三次?
yes, when one says 写了三遍, maybe he is fed up with writing 3 times since every time it takes a time, and of course maybe he is not complaining at all, but anyway, the writing process is more or less emphasized when he says 三遍.
in fact, in order to use them properly, you need to do a lot of practice, because it's a little tricky even for advanced learners. hope my answer is helpful!