By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
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
  1. import sys
  2. subjects = ["A", "B", "L", "Z", "Q", "T", "V"]
  3.  
  4. # Trial Data
  5. trials = [["Trial 1", [ 178, 206, 271, 254, 261, 218, 255]],
  6.           ["Trial 2", [ 206, 215, 221, 244, 218, 271, 215]],
  7.           ["Trial 3", [ 237, 298, 215, 233, 224, 216, 195]],
  8.           ["Trial 4", [ 198, 273, 219, 241, 218, 279, 234]],
  9.           ["Trial 5", [ 234, 226, 302, 263, 217, 275, 216]],
  10.           ["Trial 6", [ 217, 256, 227, 227, 299, 234, 229]]
  11.          ]
  12.  
  13.  
  14. # Print out trial averages
  15. for trial in trials:
  16.     trialSum = 0.0
  17.  
  18.     for measures in trial[1]:
  19.         trialSum = trialSum + measures
  20.     print "Average for %s is %.3f mg/dL." % (trial[0], trialSum/len(trial[1]))
  21.  
  22.  
  23. # Add spacing between data groups for better visual appearance.
  24. print ""
  25.  
  26. # print out subject averages
  27. for subject in subjects:
  28.     subjectIndex = subjects.index(subject)
  29.     subjectSum = 0.0
  30.  
  31.     # compute average for subject over all trials
  32.     for trial in trials:
  33.         subjectSum = subjectSum + trial[1][subjectIndex]
  34.  
  35.     # Print out average
  36.     sys.stdout.write( "Average for subject %s is %.3f mg/dL.  " %
  37.                       (subject, subjectSum/len(trials)))
  38.  
  39.     # Classify subject
  40.     if subjectSum/len(trials)>240:
  41.         print "Subject %s is high risk." % subject
  42.     elif subjectSum/len(trials)>200:
  43.         print "Subject %s is borderline high risk." % subject
  44.     else:
  45.         print "Subject %s is low risk." % subject
Nov 4 '06 #1
Share this Question
Share on Google+
1 Reply


bartonc
Expert 5K+
P: 6,596
This is a great question! With your code organized like this, you will be able to change the way you get your data more easily.

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. def GetSubjects():
  3.     return ["A", "B", "L", "Z", "Q", "T", "V"]
  4. def GetTrialData():
  5.     # Trial Data
  6.     return [["Trial 1", [ 178, 206, 271, 254, 261, 218, 255]],
  7.              ["Trial 2", [ 206, 215, 221, 244, 218, 271, 215]],
  8.              ["Trial 3", [ 237, 298, 215, 233, 224, 216, 195]],
  9.              ["Trial 4", [ 198, 273, 219, 241, 218, 279, 234]],
  10.              ["Trial 5", [ 234, 226, 302, 263, 217, 275, 216]],
  11.              ["Trial 6", [ 217, 256, 227, 227, 299, 234, 229]]
  12.              ]
  13.  
  14. def PrintTrailAverage(trials):
  15.     # Print out trial averages
  16.     for trial in trials:
  17.         trialSum = 0.0
  18.         for measures in trial[1]:
  19.             trialSum = trialSum + measures
  20.         print "Average for %s is %.3f mg/dL." % (trial[0], trialSum/len(trial[1]))
  21.     # Add spacing after this data group for better visual appearance.
  22.     print
  23. def PrintSubjectAverage(subjects, trials):
  24.     # print out subject averages
  25.     for subject in subjects:
  26.         subjectIndex = subjects.index(subject)
  27.         subjectSum = 0.0
  28.         # compute average for subject over all trials
  29.         for trial in trials:
  30.             subjectSum = subjectSum + trial[1][subjectIndex]
  31.         # Print out average
  32.         sys.stdout.write( "Average for subject %s is %.3f mg/dL. " %
  33.                          (subject, subjectSum/len(trials)))
  34.         # Classify subject
  35.         if subjectSum/len(trials)>240:
  36.             print "Subject %s is high risk." % subject
  37.         elif subjectSum/len(trials)>200:
  38.             print "Subject %s is borderline high risk." % subject
  39.         else:
  40.             print "Subject %s is low risk." % subject
  41.     # Add spacing after this data group for better visual appearance.
  42.     print
  43. def MainProgram():
  44.     s = GetSubjects()
  45.     td = GetTrialData()
  46.     PrintTrailAverage(td)
  47.     PrintSubjectAverage(s, td)
  48. MainProgram()
  49.  
Now that you see how it's done, I'll bet you can move the calculations to fuctions... Have fun and keep posting,
Barton
Nov 4 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.