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
Copy file name to clipboardExpand all lines: _sources/functions/func_Str_Cond.rst
+25-10Lines changed: 25 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,8 @@ in groups on activities and each member has an assigned role. For more informat
8
8
9
9
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
10
10
11
-
**Learning Objectives**
11
+
Learning Objectives
12
+
====================
12
13
13
14
Students will know and be able to do the following.
14
15
@@ -131,6 +132,8 @@ a string covers more than one line.
131
132
String Indices
132
133
==================
133
134
135
+
You can get a character from a string at an index (position) using ``string[index]``.
136
+
134
137
.. fillintheblank:: fsc_fitb_pogil_initials_v2
135
138
136
139
What is the last thing that will be printed when the code below runs?
@@ -141,7 +144,7 @@ String Indices
141
144
.. activecode:: fsc_ac_pogil_initials_v2
142
145
:caption: get_initials
143
146
144
-
Run the code below to see what it prints. Then fix it to pass the given test. It should return a string with the
147
+
Run the code below to see what it prints. Then fix it to pass the test shown below the code. It should return a string with the
145
148
first character of the first name and first character of the last name.
146
149
~~~~
147
150
# function definition
@@ -160,6 +163,7 @@ String Indices
160
163
# function call
161
164
main()
162
165
166
+
====
163
167
from unittest.gui import TestCaseGui
164
168
class myTests(TestCaseGui):
165
169
@@ -172,13 +176,15 @@ String Indices
172
176
173
177
.. note::
174
178
175
-
Use [index] to get a character from a string. The first character in a string is at index 0 and the last is at the length of the string minus 1 (also know as index -1 in Python).
179
+
Use ``string[index]`` to get a character from a string. The first character in a string is at index 0 and the last is at the length of the string minus 1 (also know as index -1 in Python).
176
180
177
181
Fix the function ``get_initials`` above to return a string with the first letter of the first name followed by the first letter of the last name.
178
182
179
183
String Slices
180
184
==================
181
185
186
+
You can get a copy of part or all of a string using ``str_name[start:end]``.
187
+
182
188
.. fillintheblank:: fsc_fitb_pogil_short_name
183
189
184
190
What is the last thing that will be printed when the code below runs?
@@ -217,6 +223,14 @@ String Slices
217
223
- :dna\[0\:3\]|\[:3\]: This will return a new string with the characters from index 0 to 2.
218
224
:.*: Look at the note above and try again.
219
225
226
+
.. fillintheblank:: fsc_fitb_len_string
227
+
:practice: T
228
+
229
+
What built-in function tells you the number of characters in a string?
230
+
231
+
- :len: The len function takes a string and returns the number of characters in it.
232
+
:.*: Look at the example code above.
233
+
220
234
221
235
Basic Conditionals and Tests
222
236
============================
@@ -237,13 +251,13 @@ Basic Conditionals and Tests
237
251
# function definition
238
252
def get_temp_desc(temp):
239
253
if temp < 32:
240
-
return "Baby, its cold outside!"
254
+
return "Baby, its cold outside! The temp is " + str(temp)
241
255
elif temp < 70:
242
-
return "Wear a coat"
256
+
return "Wear a coat The temp is " + str(temp)
243
257
elif temp < 80:
244
-
return "Feels great!"
258
+
return "Feels great! The temp is " + str(temp)
245
259
else:
246
-
return "Too hot to handle!"
260
+
return "Too hot to handle! The temp is " + str(temp)
247
261
248
262
# function definition
249
263
def main():
@@ -253,6 +267,10 @@ Basic Conditionals and Tests
253
267
# function call
254
268
main()
255
269
270
+
.. note::
271
+
272
+
You must first convert a number to a string using ``str(nun)`` if you want to add it to a string using ``+``.
273
+
256
274
.. fillintheblank:: fsc_fitb_pogil_elif
257
275
258
276
What keyword is used in a conditional statement when you want three of more possible outcomes?
@@ -286,9 +304,6 @@ Basic Conditionals and Tests
286
304
# function call
287
305
main()
288
306
289
-
.. note::
290
-
291
-
You must first convert a number to a string using ``str(nun)`` if you want to add it to a string using ``+``.
Copy file name to clipboardExpand all lines: pretext/functions/funcWithStrsAndConds.ptx
+30-4Lines changed: 30 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -277,13 +277,13 @@ myTests().main()
277
277
</exercise>
278
278
279
279
<note>
280
-
<p>The first character is at index 0 and an index of -1 means the last charater.</p>
280
+
<p>Use <c>string[index]</c> to get a character from a string. The first character in a string is at index 0 and the last is at the length of the string minus 1 (also know as index -1 in Python)</p>
281
281
</note>
282
282
</subsection>
283
283
<subsectionxml:id="fsc_string-slices">
284
-
<title>String Slices</title>
284
+
<title>String Slices</title>
285
+
<p>You can get a copy of part or all of a string using <c>str_name[start:end]</c>.</p>
285
286
<exerciselabel="fsc_fitb_pogil_short_name">
286
-
<p>You can get a copy of part or all of a string using <c>str_name[start:end]</c>.</p>
287
287
<statement>
288
288
<p>What is the last thing that will be printed when the code below runs? <var/> </p>
289
289
</statement>
@@ -324,7 +324,7 @@ main()
324
324
</program>
325
325
</exercise>
326
326
<note>
327
-
<p>Use the slice [start:end] operator to get a slice (substring) from a string. It will return a new string starting at the start and including all the characters up to just before the end (end - 1).</p>
327
+
<p>Use the slice [start:end] operator to get a slice (substring) from a string. It will return a new string starting at the start and including all the characters up to just before the end (end - 1). If ``start`` is missing the default is 0 and if ``end`` is missing the default value is the length of the string.</p>
328
328
</note>
329
329
<exerciselabel="fsc_fitb_three_char_slice">
330
330
<statement>
@@ -656,6 +656,32 @@ myTests().main()
656
656
</input>
657
657
</program>
658
658
</exercise>
659
+
660
+
.. activecode:: fsc_ac_or_complex_cond
661
+
:caption: complex conditional
662
+
663
+
Modify this code to use a complex conditional instead. It should still pass all tests. It should only take four lines of code or less.
0 commit comments