Skip to content

Commit c2d1893

Browse files
committed
Update Strings exercises solution for 3
The third exercise in the Strings chapter provided a solution that did not pass the tests. The previous RST version passed the tests, so I took most of the changes from there. I updated it a little bit so that the spacing of the printed message of the solution matches exactly with the presented message in the problem description. I also updated the problem description to mention that the number of 'e' characters that were found should be returned since the tests expect that behavior.
1 parent b44da0e commit c2d1893

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

pretext/Strings/Exercises.ptx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ myTests().main()
103103
<p>Assign to a variable in your program a triple-quoted string that contains
104104
your favorite paragraph of text - perhaps a poem, a speech, instructions
105105
to bake a cake, some inspirational verses, etc.</p>
106-
<p>Write a function that counts the number of alphabetic characters (a through z, or A through Z) in your text and then keeps track of how many are the letter &#x2018;e'. Your function should print an analysis of the text like this:</p>
106+
<p>Write a function that counts the number of alphabetic characters (a through z, or A through Z) in your text and then keeps track of (and returns) how many are the letter &#x2018;e'. Your function should print an analysis of the text like this:</p>
107107
<pre>Your text contains 243 alphabetic characters, of which 109 (44.8%) are 'e'.</pre>
108108
</statement>
109109
<program interactive="activecode" language="python" xml:id="ex_8_3_editor">
@@ -144,9 +144,12 @@ def count(p):
144144
if achar == 'e':
145145
numberOfe = numberOfe + 1
146146

147-
percent_with_e = (numberOfe / totalChars) * 100
148-
print("Your text contains", totalChars, "alphabetic characters of which", numberOfe, "(", percent_with_e, "%)", "are 'e'.")
149-
147+
if totalChars != 0:
148+
percent_with_e = (numberOfe / totalChars) * 100
149+
print("Your text contains", totalChars, "alphabetic characters of which", numberOfe, "(" + str(percent_with_e) + "%)", "are 'e'.")
150+
else:
151+
print("There were no characters in the input string p")
152+
return (numberOfe)
150153

151154
p = '''
152155
"If the automobile had followed the same development cycle as the computer, a

0 commit comments

Comments
 (0)