Use if(ballsPresent()) to have Karel only pick up a ball if one actually exists on that space.
Loops are essential for "all answers" seekers because they shorten your code significantly. codehs all answers karel top
Use while(frontIsClear()) to make Karel move until he hits a wall. This works regardless of the world size. Use if(ballsPresent()) to have Karel only pick up