Skip to content

Instantly share code, notes, and snippets.

@louisrawlins
Created February 28, 2017 22:33
Show Gist options
  • Save louisrawlins/26a5f49602a92c1795340e310a819175 to your computer and use it in GitHub Desktop.
Save louisrawlins/26a5f49602a92c1795340e310a819175 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'
]
# EXTRA - Lists of lists
# - How could you use this information?
# - What might be challenging, compared to books_with_titles?
# 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 - Children\'s 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. 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_titles:
print(' - ' + item)
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':
break
else:
greeting()
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment