Teacher's Guide

Chapter 8 / Level 2: Statistics Module

Teaching Objective:

  • Student learns the foundation of Modules, their uses and how to enable them
  • Understands the uses of the Statistics Module and how it augments the primary Python code-set
  • Learns all the different Statistic functions, their syntax and operation

Guide

In this exercise we’ll be working with the statistics module, this allows us to use module specific functions for producing statistic data that are not available with the stock version of Python. The same goes with the player functions we’ve been using all this time, they’re imported using the player module.

import player

import statistics

For the purpose of this exercise we will be using six (6) of the statistics module functions which are as follows:

  • statistics.mean() : Calculates the average of a set of data
  • statistics.median(): Calculates the middle value of a set of data
  • statistics.median_low(): Calculates the lowest median value of a set of data
  • statistics.median_high(): Calculates the highest median value of a set of data
  • statistics.mode(): Calculates the central tendency of a set of data, in other words the value most present in the data sample.
  • statistics.variance(): Calculates the variance of the values in a set of data. A high number means the values are spread out, a low number means the values are close together.

The objective of this level is to gather data and produce statistics from said data then have that data recorded by writing it down. Create three (3) lists named: weather_report , export_report and harvest_report ****.Walk to the three (3) light X marks and read() the files present, use the data found in these files to populate the lists.

Walk to dark X mark over the blue carpet and set up variables to store statistics data for the export_report , once set up, write() the data onto the memo on the table.


mean = statistics.mean( export_report )

median = statistics.median( export_report )

low = statistics.median_low( export_report )

high = statistics.median_high( export_report )

variance = statistics.variance( export_report )

await player.write(
  "Six month export report statistics:\nMean: %s\nMedian: %s\nLow: %s\nHigh: %s\nVariance: %s" % (mean , median , low , high , variance))

Do the same thing with harvest_report on the green carpet, setting up the variables the same way and writing them. Repeat this process with weather_report on the red carpet.

Finally, walk to the gold X marks and create a new list named group_report . On this list you will add the export_report and harvest_report in that order. Afterwards, use a list comprehension to put all elements in a single list, like this: group_report= [item for sublist in group_report for item in sublist].

Create two new variables, and add their respective statistics functions, one named variance paired with the group_report list and one named mode paired with the weather_report list. Use write() on the gold X mark with the new values to complete the level.

variance = statistics.variance(group_report)

mode = statistics.mode(weather_report)

await player.write(
  "Supplemental statistics Data:\n\nProduction variance: %s\nRainfall central tendency: %s" % (variance, mode))

Map overview and path

Map overview and path

Code Solution

import player

player.turn_right()
player.move_forward(4)
player.turn_right()
player.open()
player.collect(red_container)
player.speak(repr(type(red_container)))
player.speak(red_container)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.open()
player.collect(green_container)
player.speak(repr(type(green_container)))
player.speak(green_container)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.open()
player.collect(blue_container)
player.speak(repr(type(blue_container)))
player.speak(blue_container)

player.turn_left()
player.move_forward()
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(2)
player.turn_left()

item_id_red = id(red_container)

item_id_green = id(green_container)

item_id_blue = id(blue_container)

item_list = [ item_id_red, item_id_green , item_id_blue ]

await player.display(item_list)

player.turn_right()
player.move_forward(2)
player.turn_left()

item_attributes = repr(dir(item_list))
await player.display(item_attributes)

player.turn_right()
player.move_forward(2)
player.turn_left()

item_help = repr(help(item_list))
await player.read()
Chapter 8 / Level 2: Statistics Module | Teacher's Guide