411,078 views
38 votes
38 votes
Write a program which simulates the result of a person choosing 3 objects in a random order out of a box containing 3 objects in total. The box contains the following 3 objects: apple, ball, cat. A single selection pass of your program describes the random selection of 3 objects out of the box, without replacement. This means that a particular object can only be selected from the box once. At the beginning of a pass, the box is full, so it contains all 3 objects. After an object is selected from the box, your program will print the name of the object and then choose another object without replacing the previous object. The selection process repeats 3 times until the box is empty

User Kaushalop
by
2.4k points

1 Answer

13 votes
13 votes

Answer:

Step-by-step explanation:

The following code is written in Python, it loops through a list (box) of the objects and randomly choosing one of the objects. Prints that object out and removes it from the list. Then repeats the process until the box is empty.

import random

box = ['apple', 'ball', 'cat']

print(box)

for x in range(len(box)):

pick = random.randint(0,len(box)-1)

print("Pick " + str(x+1) + ": " + box[pick])

box.remove(box[pick])

print(box)

Write a program which simulates the result of a person choosing 3 objects in a random-example-1
User Mr Giggles
by
3.1k points