lab5-extra¶
To complete the last part of the count_sub_in_file()
task, you need to know how to catch an exception.
Relevant Socratica video:
count_sub_in_file(filename, s)
¶
Write a function count_sub_in_file(filename, s)
that takes two
arguments: the substring s (of type string) and a filename (of type
string). The function should return the number of occurrences of s
in the file given through filename
. (Note that each string object
has a method count
that should be used here.)
You can test your function count_sub_in_file
on Alice’s Adventures
in Wonderland:
How often does the substring “Alice” occur? Expect more than one hundred.
How often does the substring “Rabbit” occur? Expect a smaller number.
Modify the function count_sub_in_file(filename, s)
so that if the
file with name filename
cannot be opened, the integer value -1
is returned (instead of the number of substrings s
).
Please include the extra tasks in your file lab5.py
and submit as Computing lab5 assignment.
Back to lab5.
End of lab5-extra.