Skip to content

Commit cf3959b

Browse files
authored
Merge pull request #267 from kklamberty/fix-exercise-solution-in-strings
Update Strings exercises solution for 3 -- Thanks for the PR!
2 parents b44da0e + c2d1893 commit cf3959b

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)