In order to have Karel pick up all of the tennis balls in a location without knowing how many there are, you could use a loop control structure such as a while loop.
Here's an example of how you could write this program using a while loop in Python: look at picture
This loop will continue until Karel can no longer move forward, which will occur when Karel reaches the end of the location or encounters an obstacle. On each iteration of the loop, Karel will check to see if it is standing on a tennis ball. If it is, it will pick up the ball. If not, it will simply move forward.
This loop will continue until Karel has picked up all of the tennis balls in the location, or until it reaches the end of the location.