Skip to content

Instantly share code, notes, and snippets.

@louisrawlins
Created February 28, 2017 22:38
Show Gist options
  • Save louisrawlins/253640ca5702e78915dad3e44e014392 to your computer and use it in GitHub Desktop.
Save louisrawlins/253640ca5702e78915dad3e44e014392 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# San Francisco Public Library - Learn, Design and Build Software!
#
# Sample program created by Louis Rawlins (louis@bonfireschool.com)
# Copyright 2016, Bonfire School, covered under GPLv3
#
# Show gopher as a historical example.
# These were faster systems, Merlin, etc
# Time and fashion changes how we look for things
# Some good, some bad -- I wouldn't want to take cover photos away,
# but I do miss the speed of searching
# Concepts to cover:
# - Variables (assignment, comparison)
# - Types (string, integer)
# - Print (string, integer)
# - Input (type conversion)
# - Lists (storage, retreival, array notation)
# - Conditionals (if, else, while, for)
# - Functions (definition, calling)
# STUB - Lists of strings
books_with_titles = [
'Where the Wild Things Are',
'Over in the Meadow',
'Goodnight Moon',
'Little Bear',
'The Giving Tree'
]
# STUB - Lists of lists
books_with_dates_and_authors = [
('Where the Wild Things Are', 'Maurice Sendak', 1963),
('Over in the Meadow', 'Ezra Jack Keats', 1971),
('Goodnight Moon', 'Margaret Wise Brown', 1947),
('Little Bear', 'Else Holmelund Minarek', 1957),
('The Giving Tree', 'Shel Silverstein', 1964)
]
# STUB - Function Definition
def greeting():
# We use the print function to display our interface to the user.
# - What does '\n' mean and why is it useful?
# - How could we improve this menu?
print('\n--------------------------------------------------------------------------------')
print('San Francisco Public Library - Online Book Catalog')
print('--------------------------------------------------------------------------------\n')
print('What would you like to do?\n')
print(' 1. List all books')
print(' 2. Search books by title')
print(' 3. Search books by author')
print(' 4. Search books by date')
print(' 5. Exit')
# STUB - Function Calls
greeting()
# STUB - Conditional Statements
while True:
user_choice = input('\nPlease select (? for help): ')
# STUB - Conditional Statements: if, elif and else
if user_choice == '1':
# How could we improve the readability of this book list?
print('\n--------------------------------------------------------------------------------')
print('All books')
print('--------------------------------------------------------------------------------')
for item in books_with_dates_and_authors:
print(' - ' + item[0] + ' (' + str(item[2]) + ') by ' + item[1])
elif user_choice == '2':
print('\n--------------------------------------------------------------------------------')
print('Search books by title')
print('--------------------------------------------------------------------------------')
search_term = input('Enter title to search: ')
matching_books = []
print('\n--------------------------------------------------------------------------------')
print('Search results for "' + search_term + '"')
print('--------------------------------------------------------------------------------')
for item in books_with_titles:
# Search is case sensitive.
# - What happens when you search for "the" or "The"?
# - How could you change this to find both?
if search_term in str(item):
title_position = books_with_titles.index(item)
matching_books.append(books_with_titles[title_position])
if matching_books == []:
print('No Results\n')
else:
for item in matching_books:
print(' - ' + item)
elif user_choice == '3':
print('\n--------------------------------------------------------------------------------')
print('Search books by author')
print('--------------------------------------------------------------------------------')
search_term = input('Enter title to search: ')
matching_books = []
print('\n--------------------------------------------------------------------------------')
print('Search results for "' + search_term + '"')
print('--------------------------------------------------------------------------------')
for item in books_with_dates_and_authors:
# Search happens over a list of lists.
# - How is this different from title search?
if search_term in str(item[1]):
title_position = books_with_dates_and_authors.index(item)
matching_books.append(books_with_dates_and_authors[title_position])
if matching_books == []:
print('No Results\n')
else:
for item in matching_books:
title_position = matching_books.index(item)
book_title = matching_books[title_position][0]
book_author = matching_books[title_position][1]
# The book date is an integer.
# - What does the str() function do here?
book_date = str(matching_books[title_position][2])
print(' - ' + book_title + ' (' + book_date + ') by ' + book_author)
elif user_choice == '4':
print('\n--------------------------------------------------------------------------------')
print('Search books by date')
print('--------------------------------------------------------------------------------')
# STUB - functions and return values
def search_date():
while True:
start_year = input('Enter start year: ')
end_year = input('Enter end year: ')
if end_year < start_year:
print('Please try a new date range. The end year is before the start year.')
else:
# Input from the user is entered as a string.
# - What does the int() function do here?
# - What does it mean to return something in a function?
return (int(start_year), int(end_year))
def search_results_by_date():
search_range = search_date()
start_year = search_range[0]
end_year = search_range[1]
matching_books = []
print('\n--------------------------------------------------------------------------------')
print('Search results from ' + str(start_year) + ' to ' + str(end_year))
print('--------------------------------------------------------------------------------')
for item in books_with_dates_and_authors:
# Search happens over a list of lists.
# - How is this different from title search?
if start_year < item[2] < end_year:
title_position = books_with_dates_and_authors.index(item)
matching_books.append(books_with_dates_and_authors[title_position])
if matching_books == []:
print('No Results\n')
else:
for item in matching_books:
title_position = matching_books.index(item)
book_title = matching_books[title_position][0]
book_author = matching_books[title_position][1]
# The book date is an integer.
# - What does the str() function do here?
book_date = str(matching_books[title_position][2])
print(' - ' + book_title + ' (' + book_date + ') by ' + book_author)
search_results_by_date()
elif user_choice == '5':
break
else:
greeting()
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment