Python Basics and Biostatistics

Tallying Results

Back to Index
In this lesson the student will learn how to:
  1. Implement a never ending loop
  2. End a loop at any point within it's execution
  3. Add values to a list
  4. Use the raw_input() function
By the end of this lesson the student will be able to:

  Write a script which tallies measurments and calculates the 
  mean using this tally.

Let's say that the state decides for some reason that it needs to know the height of all the nine-year-old girls in the town of Trona. So, some teacher or administrator or public health official is given the task of measuring all the nine-year-old girls attending Trona Elementary School. In the process of recording these measurements this person might create a tally that looks something like this:

Height of Nine-Year-Old Girls in Trona, 2003:

 height    number
   3'11"     1
   4' 0"     2
   4' 1"     1
   4' 2"     0
   4' 3"     2
   4' 4"     2
   4' 5"     1
   4' 6"     0
   4' 7"     1
   4' 8"     0
   4' 9"     2
   4'10"     1
   4'11"     0
   5' 0"     0
   5' 1"     1

Using this data we can easily calculate an average height after converting it into inches.

47+48+48+49+51+51+52+52+53+55+57+57+58+61 = 739
  739 / 14 = 52.79

Here's a little script which may make tallying our information a little easier:
#!/usr/bin/python heights = [] amt = [] title = raw_input("Enter Tally Title: ") ht = 0 n = 0 #populate our lists while True: ht = raw_input("Enter height in inches (hit q to quit): ") if(ht == 'q'): break else: heights.append(ht) n = raw_input("Enter number of subjects for that height: ") amt.append(n) #print the lists print "--------------\n\n\n" print title print "\n" print "HEIGHT NUMBER" for i in range(0,len(heights)): print str(heights[i]) + ": " + str(amt[i])
Sample output:

Height of Dwarfs in Fairyland

HEIGHT  NUMBER
14      2
16      3
17      1
19      2
21      1

Inspect the script shown above (after executing it a few times) and consider the following points:
When it comes to converting a value in feet-inches format to straight inches, the following trick will come in handy. (Run this script to see the output it produces. Also change the input to 5'11" to see how it deals with a two-digit inches value.)
#!/usr/bin/python input = "5'3\"" print input feet = input[0] single = input.index("'") print single double = input.index('"') print double inches = input[single+1:double] print inches

ASSIGNMENT:

Extend the sample script so that it calculates the mean for your data points. Take input for height in feet and inches and convert it to straight inches. Report the mean after displaying your tally. (NOTE: You will need to use the int() and the float() functions in order to convert the input to numbers so you can perform the arithmetic.)