114,949 views
1 vote
1 vote
Say you want to write a program to have Karel to pick up all of the tennis balls in a location, but you don't know how many tennis balls there are. What control structure would you use?

User Isquierdo
by
2.9k points

1 Answer

27 votes
27 votes
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.
Say you want to write a program to have Karel to pick up all of the tennis balls in-example-1
User Tao Zhyn
by
3.1k points