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. -
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
1 1342
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. -
import sys
-
def GetSubjects():
-
return ["A", "B", "L", "Z", "Q", "T", "V"]
-
def GetTrialData():
-
# Trial Data
-
return [["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]]
-
]
-
-
def PrintTrailAverage(trials):
-
# 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 after this data group for better visual appearance.
-
print
-
def PrintSubjectAverage(subjects, trials):
-
# 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
-
# Add spacing after this data group for better visual appearance.
-
print
-
def MainProgram():
-
s = GetSubjects()
-
td = GetTrialData()
-
PrintTrailAverage(td)
-
PrintSubjectAverage(s, td)
-
MainProgram()
-
Now that you see how it's done, I'll bet you can move the calculations to fuctions... Have fun and keep posting,
Barton
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
by: Richard Hayden |
last post by:
Hi,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
|
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...
|
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...
|
by: Dave |
last post by:
Hello all,
Please consider this code:
#ifndef FOO_INCLUDED
#define FOO_INCLUDED
// File: foo.h
class foo
{
|
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...
|
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...
|
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
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |