Exercise 16: Sample Me This

So, the Challenge of the previous exercise has passed. Hopefully, you completed it without peeking at the solution. If not, it's okay -- you'll get more practice and get better.

It's time to play a statistics game called Sample Me This. The game works like this:

  1. You are shown a population of data.
  2. You are shown a sample taken of the population.
  3. You have to guess the sampling method utilized.

Sounds like fun, doesn't it? I'm sure you're just as excited as I am.

But wait! There's more!

We can't just go play the game. We must create it first.

Writing the game

In a new file named ex16-sample-game.py, type this:

import random

    data = ['Land Rover', 'Mercedes', 'Ford', 'Tesla', 'GMC', 'Toyota']

    def choiceA():
        print data[::2]
        return 'iterate'

    def choiceB():
        print random.sample(data, 3)
        return 'random'

    def choiceC():
        print data[:3]
        return 'front slice'

    def choiceD():
        print data[2:5]
        return 'middle slice'

    def choiceE():
        print data[-3:]
        return 'back slice'

    value = random.random()

    if value < 0.2:
        answer = choiceA()
    elif value < 0.4:
        answer = choiceB()
    elif value < 0.6:
        answer = choiceC()
    elif value < 0.8:
        answer = choiceD()
    else:
        answer = choiceE()

    print "Given this population:"
    print data
    print 'Guess the sampling method. Choices:\n'
    print '1.iterate\n2.random\n3.front slice\n4.middle slice\n5.back slice'
    playerGuess = raw_input('>> ')


    if answer == playerGuess:
        print "Correct! The sampling method was %s" % answer
    else:
        print "Oops. That's incorrect."

What you should see

Output:

['Land Rover', 'Ford', 'GMC']
Given this population:
['Land Rover', 'Mercedes', 'Ford', 'Tesla', 'GMC', 'Toyota']
Guess the sampling method. Choices:

1.iterate
2.random
3.front slice
4.middle slice
5.back slice
>>
Correct! The sampling method was iterate

Creative Commons License
Learn Stats in 10,000 Hours by Jonathan B. Miller is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
comments powered by Disqus