Update boxes.py

Replace some 'while' with 'if'