Example Function Structure
All functions should have a triple-quoted comment directly under the
def
line. This comment should describe:
-
what the function does
-
what each parameter is and its type
-
what the function returns
Here are two examples of well-defined functions:
def letter_count(letter,text):
"""
This function counts the number of occurrences of a given letter in
a given text.
param letter: the letter to search for (type: a single char string)
param text: the text to search in (type: string)
returns: the number of times the letter occurrs in text (type: int)
"""
count = 0
for i in range(len(text)):
if text[i] == letter:
count = count + 1
return count
def print_rules():
"""
Prints the rules of the game to the screen.
params: None
returns: None
"""
print("Welcome to the game of life!")
print("Here are the rules: ")
print("Rule #1: be nice to others")
print("Rule #2: don't give up")
print("Rule #3: do your best")