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:
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.
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."
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