Python Basics and Biostatistics
Tallying Results
Back to Index
In this lesson the student will learn how to:
- Implement a never ending loop
- End a loop at any point within it's execution
- Add values to a list
- 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:
- Infinite Loop - The while True: loop in this script
will never end unless a certain condition is met. It will continue on forever (or until the power goes out).
The only thing that will stop it (besides pulling the plug out of the back
of your computer or an interrupt signal from the operating system) is if the
user enters a single 'q' when prompted to enter a height. This will trigger
a call to the break statement. The break statement will end this otherwise
infinite loop.
- The raw_input Function - The raw_input function is the
counter-part to the input function, which you should feel free to experiment
with, but long story made short: the raw_input function will work for this
assignment better than the input function.
- Adding Values to a List - The append() function allows us to add
values to a list. The most recently appended value occupies the last
position in the list (unless we sort it or do something to alter the order
of items in the list).
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.)