473,394 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Functions (organizing inline code)

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
1 1342
bartonc
6,596 Expert 4TB
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

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

Similar topics

13
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg....
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
21
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've...
7
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must...
3
by: Dave | last post by:
Hello all, Please consider this code: #ifndef FOO_INCLUDED #define FOO_INCLUDED // File: foo.h class foo {
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
4
by: John Goche | last post by:
Hello, I have been going through several header source files which define classes with inline functions. In the case where the inline functions are not defined in place, the inline functions...
2
by: aaragon | last post by:
Hi everyone, I would like to create a very simple function: // header file inline void point_map() { PointMap pointMap = get(vertex_point_t(), g); } // main.cpp
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.