You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> 1. Write a function definition to calculate the volume of a cuboid. The function will use three parameters `H`, `W` and `L` and return the volume.
103
+
> 1. Write a function definition to calculate the volume of a cuboid. The function will use three parameters `h`, `w`
104
+
> and `l` and return the volume.
104
105
>
105
106
> 2. Supposing that in addition to the volume I also wanted to calculate the surface area and the sum of all of the edges. Would I (or should I) have three separate functions or could I write a single function to provide all three values together?
106
107
>
107
108
> > ## Solution
108
-
> >
109
+
> > - A function to calculate the volume of a cuboid could be:
110
+
> >
111
+
> > ~~~
112
+
> > def calculate_vol_cuboid(h, w, l):
113
+
> > """
114
+
> > Calculates the volume of a cuboid.
115
+
> > Takes in h, w, l, that represent height, width, and length of the cube.
116
+
> > Returns the volume.
117
+
> > """
118
+
> > volume = h * w * l
119
+
> > return volume
109
120
> > ~~~
110
-
> > def cuboid(H,W,L):
111
-
> > vol = H*W*L
112
-
> > sa = 2*(H*W + H*L + L*W)
113
-
> > edges = 4*(H + W + L)
114
-
> > return vol, sa, edges
121
+
> > {: .language-python}
122
+
> > - It depends. As a rule-of-thumb, we want our function to __do one thing and one thing only, and to do it well.__
123
+
> > If we always have to calculate these three pieces of information, the 'one thing' could be
124
+
> > 'calculate the volume, surface area, and sum of all edges of a cube'. Our function would look like this:
> > print("Volume = ", volume, " Surface_area = ", surface_area, " Sum of edges = ", length_of_edges)
126
+
> > ~~~
127
+
> > # Method 1 - single function
128
+
> > def calculate_cuboid(h, w, l):
129
+
> > """
130
+
> > Calculates information about a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
131
+
> >
132
+
> > Returns the volume, surface area, and sum of edges of the cuboid.
133
+
> > """
134
+
> > volume = h * w * l
135
+
> > surface_area = 2 * (h * w + h * l + l * w)
136
+
> > edges = 4 * (h + w + l)
137
+
> > return volume, surface_area, edges
118
138
> > ~~~
119
139
> > {: .language-python}
120
140
> >
121
-
> > The cuboid function above returns all three values from a single call. This means that you need three variables in which to place the 3 returned values. If you do not provide three variables, you will get an error.
122
-
> > Unless you are always going to use the 3 values, it would probably be better in this case to use distinct functions for these three cases.
141
+
> > It may be better, however, to break down our function into separate ones - one for each piece of information we are
142
+
> > calculating. Our functions would look like this:
143
+
> > ~~~
144
+
> > # Method 2 - separate functions
145
+
> > def calc_volume_of_cuboid(h, w, l):
146
+
> > """
147
+
> > Calculates the volume of a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
148
+
> > """
149
+
> > volume = h * w * l
150
+
> > return volume
151
+
> >
152
+
> >
153
+
> > def calc_surface_area_of_cuboid(h, w, l):
154
+
> > """
155
+
> > Calculates the surface area of a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
156
+
> > """
157
+
> > surface_area = 2 * (h * w + h * l + l * w)
158
+
> > return surface_area
159
+
> >
160
+
> >
161
+
> > def calc_sum_of_edges_of_cuboid(h, w, l):
162
+
> > """
163
+
> > Calculates the sum of edges of a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
164
+
> > """
165
+
> > sum_of_edges = 4 * (h + w + l)
166
+
> > return sum_of_edges
167
+
> > ~~~
168
+
> > {: .language-python}
169
+
> >
170
+
> > We could then rewrite our first solution:
171
+
> > ~~~
172
+
> > def calculate_cuboid(h, w, l):
173
+
> > """
174
+
> > Calculates information about a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
175
+
> >
176
+
> > Returns the volume, surface area, and sum of edges of the cuboid.
@@ -139,9 +201,9 @@ We need to go through a 2-step process before we can use them in our own program
139
201
Step 1. use the `pip` command from the commandline. `pip` is installed as part of the Python install and is used to fetch the package from the Internet and install it in your Python configuration.
140
202
141
203
~~~
142
-
> pip install <package name>
143
-
{: .bash}
204
+
$ pip install <package name>
144
205
~~~
206
+
{: .language-bash}
145
207
146
208
pip stands for Python install package and is a commandline function. Because we are using the Anaconda distribution of Python, all of the packages that we will be using in this lesson are already installed for us, so we can move straight on to step 2.
0 commit comments