Our task is to create a turtle that always stays within a rectangle.
It would be really great if you could show me how I can make a turtle run away from a line another turtle has created.
Please don't fix the problem for me.
First, you need the bounds of the rectangle in some form -- it can be the lower left position plus a width and height or it can be the lower left position and the upper right position, etc.  (It could even be the formulas of the four lines that make up the rectangle.)
Then write a predicate function that tests if an (x,y) position is fully within the rectangle or not.  You can simply do a series of comparisons to make sure x is greater than the lower left x and less than the upper right x, and ditto for y.  Typically returning True or False.
If the predicate returns False, indicating you've touched or crossed some line of the rectangle, then turn around and go in the opposite direction (or some other recovery technique.)  You can also consider first using turtle's undo feature to eliminate the move that made you touch the line.
If you'd like example code that does the above, please indicate such.
How can I make a turtle not touch a line?