- Exercise: referring back to MakingLibrarySoftware1, fill in these functions:
def count_word(paragraph, word):
'''counts the number of times a word appears in a paragraph'''
pass
def capitalize_word(paragraph, word):
'''capitalizes all instances of a word in a paragraph'''
pass
def exclamize(paragraph):
'''makes every sentence in a paragraph an exclamation!'''
pass
def sort_words(paragraph):
'''sort the words in a paragraph'''
pass
now, write a script that allows a user to type in a paragraph and runs each function on that paragraph, in order.
- Questions from last time? Progress?
- Functions:
- positional arguments
- keyword arguments
- variable arguments
- doc string
- Classes python.org tutorial on classes
- attributes
- methods
- Saving your data with
pickledocumentation - Exercise: write a class to contain library data…
Here's an example of the exercise from this session worked out…
def count_word(paragraph, word):
'''counts the number of times a word appears in a paragraph'''
words = paragraph.split()
return words.count(word)
def capitalize_word(paragraph, word):
'''capitalizes all instances of a word in a paragraph'''
paragraph = paragraph.replace(word,word.capitalize())
return paragraph
def exclamize(paragraph):
'''makes every sentence in a paragraph an exclamation!'''
paragraph = paragraph.replace('.','!')
paragraph = paragraph.replace('?','!')
return paragraph
def sort_words(paragraph):
'''sort the words in a paragraph'''
words = paragraph.split()
words.sort()
return words