Skip to content

Commit aa7652a

Browse files
authored
Update I Best Practices.py
1 parent 8eb2c99 commit aa7652a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Writing Functions in Python/I Best Practices.py

+24
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,27 @@ def func_name(arguments):
3232
import inspect
3333
print(inspect.getdoc(func_name))
3434
**************************************************************************************************************************************************************"""
35+
## Crafting a docstring
36+
37+
# Add a docstring to count_letter()
38+
def count_letter(content, letter):
39+
"""Count the number of times `letter` appears in `content`.
40+
41+
# Add a Google style arguments section
42+
Args:
43+
content (str): The string to search.
44+
letter (str): The letter to search for.
45+
46+
# Add a returns section
47+
Returns:
48+
int
49+
50+
# Add a section detailing what errors might be raised
51+
Raises:
52+
ValuError: If `letter` is not a one-character string
53+
"""
54+
if (not isinstance(letter, str)) or len(letter) != 1:
55+
raise ValueError('`letter` must be a single character string.')
56+
return len([char for char in content if char == letter])
57+
58+

0 commit comments

Comments
 (0)