449,215 Members | 1,951 Online
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

# Functions (organizing inline code)

 P: 14 1.How do I get functions to help me simplify access to the data structure. 2. How do I get functions to encapsulate the various calculations that I have to make. 3. How do I get functions to print out the computed information. Expand|Select|Wrap|Line Numbers import sys subjects = ["A", "B", "L", "Z", "Q", "T", "V"]   # Trial Data trials = [["Trial 1", [ 178, 206, 271, 254, 261, 218, 255]],           ["Trial 2", [ 206, 215, 221, 244, 218, 271, 215]],           ["Trial 3", [ 237, 298, 215, 233, 224, 216, 195]],           ["Trial 4", [ 198, 273, 219, 241, 218, 279, 234]],           ["Trial 5", [ 234, 226, 302, 263, 217, 275, 216]],           ["Trial 6", [ 217, 256, 227, 227, 299, 234, 229]]          ]     # Print out trial averages for trial in trials:     trialSum = 0.0       for measures in trial[1]:         trialSum = trialSum + measures     print "Average for %s is %.3f mg/dL." % (trial[0], trialSum/len(trial[1]))     # Add spacing between data groups for better visual appearance. print ""   # print out subject averages for subject in subjects:     subjectIndex = subjects.index(subject)     subjectSum = 0.0       # compute average for subject over all trials     for trial in trials:         subjectSum = subjectSum + trial[1][subjectIndex]       # Print out average     sys.stdout.write( "Average for subject %s is %.3f mg/dL.  " %                       (subject, subjectSum/len(trials)))       # Classify subject     if subjectSum/len(trials)>240:         print "Subject %s is high risk." % subject     elif subjectSum/len(trials)>200:         print "Subject %s is borderline high risk." % subject     else:         print "Subject %s is low risk." % subject Nov 4 '06 #1