# Lab 10 - Data Types

Sue Evans

Press space bar for next screen

# Objectives

• Practice using the Python Dictionary type.
• Practice using Python's List type.
• Use top-down design to identify the necessary functions
• Program incrementally using stubs.

# Step 0 - Set up

• Make a lab10 directory in your 201/labs directory
• Change into your lab10 directory.
• Copy the file called examData.dat from Mr. Lupoli's pub directory into your lab 10 directory.
cp /afs/umbc.edu/users/s/l/slupoli/pub/labCode201/examData.dat .
• Use emacs to create a lab10.py file in your lab10 directory

# Exam Data

• You'll be writing a program that reads data from a file that consists of a student's name and their exam score on each line and stores it in a dictionary.
• ```George 95
Martha 83
Dick 57
Jane 99
John 17
Kate 17
Homer 70
Marge 77
```
• You'll then find some statistical information about the exam by examining the exam scores. We're interested in the mean, minimum and maximum values. Most of these are trivial to find from a sorted list.

# Step 1 - Design

• Let's work on the design by examining the sequence of events and using top-down design.
1. Print a Greeting
2. Prompt for filename
3. Open the file
4. Read a name & score into the dictionary
5. Repeat step 4 until EOF
6. Close the file
7. Use values() to make a list of the scores
8. Sort the list of scores
9. Find the exam statistics
• By choosing the data types carefully, we can limit the amount of code needed.
• We should have :
• printGreeting()
• readFile() takes a filename and returns a dictionary. It should implement steps 3 - 6.
• findMean() takes a list of scores and returns the average.

# Step 2 - main() & stubs

### Write the top-level functions

• You can easily write printGreeting()
• Write a stub for readFile(filename)
• Write a stub for findMean(scores)
• Write main() that includes asking the user for the filename and has calls to these three functions.

### Testing Time

• Run your program now and clean up any syntax errors or typos.
• Make sure the stubs are printing something.

• readFile() should take a filename as its only argument. You should open the file for reading and then read a line at a time, splitting each line into a name and score, and storing the pair into a dictionary.
• Don't forget to close the file, before returning the dictionary.

### Testing Time

• Run your program now and clean up any syntax errors or typos.
• Where will any errors be ?
• In readFile() or main, of course.
• We don't need to look at printGreeting(), since we cleaned up all of the errors in that fuction earlier.
• Once you have this running, look at the dictionary that was returned to main() to make sure it holds what it should.

# Step 4 - findMean()

• findMean(scores) takes as list of scores, finds the average of them and returns it.
• Write it now.
• You can test it independently by passing it the list :
[1, 2, 3, 4, 5]   It should return 3, of course.

# Step 5 - complete main()

• The next step is to modify main() so that in addition to making function calls, it is also doing tasks 7, 8 and 9 from our sequence of tasks. They are :
• Use values() to make a list of the scores from the dictionary
• Sort the list of scores
• Find the remaining exam statistics: minimum and maximum.
• Test the completed program.

# Bonus Step

Prompt for a name of a student in the file and print if they are above or below the mean.

# Challenge Step - Standard Deviation

Find the standard deviation of the scores.

The standard deviation is a measure of how spread out the data is relative to the mean. If the data is tightly clustered around the mean then the standard deviation is small. When the data is more spread out, the standard deviation is large.

Write a function findStdDev() that calculates the standard deviation of the scores. Here's the formula:

where bar x represents the mean,
x i represents the ith data value,
and n is the number of data values.

Sigma means the sum of whatever follows it.