From 5382061de0db4633ca553be428cdb6922c8b54a5 Mon Sep 17 00:00:00 2001 From: navarroalan55 Date: Sun, 11 Dec 2016 18:25:02 -0500 Subject: [PATCH 1/8] updated functions --- library.py | 149 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 107 insertions(+), 42 deletions(-) diff --git a/library.py b/library.py index 5216eb6..3c1c75f 100644 --- a/library.py +++ b/library.py @@ -1,88 +1,153 @@ -import json - +import json def open_library(filename): - # Create empty dictionaries just in case the library file is empty + students = {} - books = {} - # Open the library file encoded in JSON and load it into the data object - # We use the with keyword so we don't have to explicitly close the file - # later. - # - # Alternatively you could use: - # - # f = open(filename) - # data = json.load(f) - # f.close() - # - # and accomplish the same thing. + books = {} with open(filename) as f: data = json.load(f) - # If there are students or books in the library, - # overwrite the empty dictionaries we created if data['students'] != {}: students = data['students'] if data['books'] != {}: books = data['books'] - # Return the data we loaded from the file - return students, books - + return students,books def add_book(filename, isbn, title, author): - # Here's a start + students, books = open_library(filename) - # Now how can we add books to the data? - # In the space below, write code that adds the key isbn - # and the value {'title':title, 'author':author} - # to the books object. + isbn=input("what is the isbn?") + title=input("what is the title?") + author=input("who is the author?") + + books[isbn] = {'title': title, 'author':author} + - # Finally, write code that writes the new data to the library - # Do we need to return anything? - pass + print("Book has been sucesfully added to the library!!") + + + with open(filename) as f: + json.dump({'students': students, 'books': books}, f) def remove_book(filename, isbn): + students, books = open_library(filename) - # How can we *remove* an item from a dictionary? - # Write code to delete the book keyed by isbn in the space below + for key in books: + + print(key,books[key]) + print('') + print('') + + isbn = input("what is the isbn of the book you wish to delete?") + + if isbn in books: + del books[isbn] + + print("book has been deleted") - # Now write code that saves the new version of the data to your library - pass + with open(filename) as f: + json.dump({'students': students, 'books': books}, f) def check_out(filename, isbn, s_id): students, books = open_library(filename) # Find a way to mark a book as checked out. Be sure to associate # the book with the student who borrowed it! + print("ALL BOOKS IN LIBRARY") + for key in books: + print(key, books[key]) + print('') + print('') - # And again save the data here + student_name=input("what is your name?") + s_id=input("what is ur ID?") + + isbn=input('what book do you want to check out(choose by isbn)?') + + if isbn not in books: + print ("Book is not in Library") + else: + books[isbn] = {'status': "checked out"} + print('%s has been checked out' % isbn) + students = {student_name: s_id} - pass -def return_book(filename, isbn): + # And again save the data here + + with open(filename) as f: + json.dump({'students': students, 'books': books}, f) + +def return_book(filename,isbn): students, books = open_library(filename) + isbn=input("what book do you want to return?") + + if books[isbn]:{'status':"checked in"} + print("book is already checked in" ) + + + if isbn in books: + books[isbn] = {'status': "checked in"} + else: + print("book is not in Library") # Now ensure that the book is no longer checked out and save the changes # to the library. - - pass + with open(filename) as f: + json.dump({'students': students, 'books': books}, f) -def status(filename): +def status(filename,isbn): students, books = open_library(filename) # Print out two lists - one of all books currently checked out, # and one of all available books. - - pass - - + print(" ALL BOOKS IN LIBRARY:") + for key in books: + + print(key,books[key]) + print('') + print('') + + print (" BOOKS THAT HAVE BEEN CHECKED OUT:") + + +# Main loofor key, value in mydic.iteritems() : + + + +while True: + print('=' * 21) + print(' Library System') + print('=' * 21) + print('1. Add books to the library') + print('2. Remove books from library ') + print('3. Check out book from library ') + print('4. Return book') + print('5. Library status') + print('Q. Quit') + m = input('Select an option from above or enter Q to quit. ') + if m.upper() == 'Q': + break + + # replace 'pass' with appropriate inputs and function calls. + elif m == '1': + add_book('data/test.json','isbn','title','author') + elif m == '2': + remove_book('data/test.json','isbn') + elif m == '3': + check_out('data/test.json','isbn','s_id') + elif m == '4': + return_book('data/test.son','isbn') + elif m == '5': + status('data/test.json','isbn') + else: + print('Invalid selection.') From 069e3d78275095cfc6d2f9559252c60e470e0d0c Mon Sep 17 00:00:00 2001 From: navarroalan55 Date: Thu, 15 Dec 2016 11:25:42 -0500 Subject: [PATCH 2/8] Update library.py --- library.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/library.py b/library.py index 3c1c75f..949ff1c 100644 --- a/library.py +++ b/library.py @@ -31,7 +31,7 @@ def add_book(filename, isbn, title, author): print("Book has been sucesfully added to the library!!") - with open(filename) as f: + with open(filename,'w') as f: json.dump({'students': students, 'books': books}, f) @@ -53,7 +53,7 @@ def remove_book(filename, isbn): print("book has been deleted") - with open(filename) as f: + with open(filename,'w') as f: json.dump({'students': students, 'books': books}, f) def check_out(filename, isbn, s_id): @@ -76,7 +76,7 @@ def check_out(filename, isbn, s_id): if isbn not in books: print ("Book is not in Library") else: - books[isbn] = {'status': "checked out"} + books[isbn]['checked_out']=s_id print('%s has been checked out' % isbn) students = {student_name: s_id} @@ -84,25 +84,25 @@ def check_out(filename, isbn, s_id): # And again save the data here - with open(filename) as f: + with open(filename,'w') as f: json.dump({'students': students, 'books': books}, f) def return_book(filename,isbn): students, books = open_library(filename) isbn=input("what book do you want to return?") - if books[isbn]:{'status':"checked in"} + if books[isbn]['checked_out]='' print("book is already checked in" ) if isbn in books: - books[isbn] = {'status': "checked in"} + books[isbn]['checked_out']=s_id else: print("book is not in Library") # Now ensure that the book is no longer checked out and save the changes # to the library. - with open(filename) as f: + with open(filename,'w') as f: json.dump({'students': students, 'books': books}, f) From d24c92ff988aece44f143d698c29c19473ea492f Mon Sep 17 00:00:00 2001 From: navarroalan55 Date: Thu, 15 Dec 2016 11:33:09 -0500 Subject: [PATCH 3/8] Update library.py --- library.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library.py b/library.py index 949ff1c..b766179 100644 --- a/library.py +++ b/library.py @@ -91,12 +91,12 @@ def return_book(filename,isbn): students, books = open_library(filename) isbn=input("what book do you want to return?") - if books[isbn]['checked_out]='' + if books[isbn] print("book is already checked in" ) - if isbn in books: - books[isbn]['checked_out']=s_id + if ['checked_out'] in books[isbn]: + del books[isbn]['checked_out'] else: print("book is not in Library") @@ -118,6 +118,7 @@ def status(filename,isbn): print('') print (" BOOKS THAT HAVE BEEN CHECKED OUT:") + print(books[isbn]['checked_out']) # Main loofor key, value in mydic.iteritems() : From 5498ad7b799417253d3e7ca3e4320bbea9c237ee Mon Sep 17 00:00:00 2001 From: navarroalan55 Date: Thu, 15 Dec 2016 17:47:44 -0500 Subject: [PATCH 4/8] IT WORKS!!! but .... couldn't get for status to print books checked out, everything else WORKS!!!!! --- library.py | 99 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/library.py b/library.py index b766179..7200c3c 100644 --- a/library.py +++ b/library.py @@ -1,7 +1,7 @@ import json -def open_library(filename): +def open_library(filename): students = {} books = {} @@ -15,33 +15,29 @@ def open_library(filename): if data['books'] != {}: books = data['books'] - return students,books + return students, books -def add_book(filename, isbn, title, author): +def add_book(filename, isbn, title, author): students, books = open_library(filename) - isbn=input("what is the isbn?") - title=input("what is the title?") - author=input("who is the author?") - - books[isbn] = {'title': title, 'author':author} + isbn = input("what is the isbn?") + title = input("what is the title?") + author = input("who is the author?") + books[isbn] = {'title': title, 'author': author} print("Book has been sucesfully added to the library!!") - - with open(filename,'w') as f: + with open(filename, 'w') as f: json.dump({'students': students, 'books': books}, f) def remove_book(filename, isbn): - students, books = open_library(filename) for key in books: - - print(key,books[key]) + print(key, books[key]) print('') print('') @@ -52,10 +48,10 @@ def remove_book(filename, isbn): print("book has been deleted") - - with open(filename,'w') as f: + with open(filename, 'w') as f: json.dump({'students': students, 'books': books}, f) + def check_out(filename, isbn, s_id): students, books = open_library(filename) @@ -67,58 +63,67 @@ def check_out(filename, isbn, s_id): print('') print('') + s_id = input("what is ur ID?") - student_name=input("what is your name?") - s_id=input("what is ur ID?") - - isbn=input('what book do you want to check out(choose by isbn)?') + isbn = input('what book do you want to check out(choose by isbn)?') if isbn not in books: - print ("Book is not in Library") + print("Book is not in Library") else: - books[isbn]['checked_out']=s_id + books[isbn]['checked_out'] = s_id print('%s has been checked out' % isbn) - students = {student_name: s_id} - - # And again save the data here - with open(filename,'w') as f: + with open(filename, 'w') as f: json.dump({'students': students, 'books': books}, f) -def return_book(filename,isbn): - students, books = open_library(filename) - isbn=input("what book do you want to return?") - if books[isbn] - print("book is already checked in" ) +def return_book(filename, isbn): + students, books = open_library(filename) + isbn = input("what book do you want to return(by isbn)?") + if books[isbn]['checked_out']: + del books[isbn]['checked_out'] + print("%s has been returned" % isbn) - if ['checked_out'] in books[isbn]: - del books[isbn]['checked_out'] else: - print("book is not in Library") + print("book is not in Library or book is already checked in") # Now ensure that the book is no longer checked out and save the changes # to the library. - with open(filename,'w') as f: + with open(filename, 'w') as f: json.dump({'students': students, 'books': books}, f) -def status(filename,isbn): +def status(filename, isbn, ): students, books = open_library(filename) # Print out two lists - one of all books currently checked out, # and one of all available books. - print(" ALL BOOKS IN LIBRARY:") - for key in books: - print(key,books[key]) - print('') - print('') - print (" BOOKS THAT HAVE BEEN CHECKED OUT:") - print(books[isbn]['checked_out']) + print("Do you want:") + print("1. ALL BOOKS") + print("2. BOOKS CHECKED OUT") + print("choose 1 or 2") + option = input("") + if option == '1': + + print(" ALL BOOKS IN LIBRARY:") + for key in books: + print(key, books[key]) + print('') + print('') + + elif option == '2': + print(" BOOKS THAT HAVE BEEN CHECKED OUT:") + + if books[isbn]["check_out"]: + for key in books: + print(key, books[isbn]["check_out"]) + + else: + print('Invalid selection.') # Main loofor key, value in mydic.iteritems() : @@ -141,14 +146,14 @@ def status(filename,isbn): # replace 'pass' with appropriate inputs and function calls. elif m == '1': - add_book('data/test.json','isbn','title','author') + add_book('data/test.json', 'isbn', 'title', 'author') elif m == '2': - remove_book('data/test.json','isbn') + remove_book('data/test.json', 'isbn') elif m == '3': - check_out('data/test.json','isbn','s_id') + check_out('data/test.json', 'isbn', 's_id') elif m == '4': - return_book('data/test.son','isbn') + return_book('data/test.json', 'isbn') elif m == '5': - status('data/test.json','isbn') + status('data/test.json', 'isbn', ) else: print('Invalid selection.') From 798964e863dc227af008cc7728ce4fae4cf1b3da Mon Sep 17 00:00:00 2001 From: navarroalan55 Date: Fri, 16 Dec 2016 15:01:30 -0500 Subject: [PATCH 5/8] Update library.py --- library.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.py b/library.py index 7200c3c..9aff805 100644 --- a/library.py +++ b/library.py @@ -121,7 +121,7 @@ def status(filename, isbn, ): if books[isbn]["check_out"]: for key in books: print(key, books[isbn]["check_out"]) - + http://stackoverflow.com/questions/3817529/syntax-for-creating-a-dictionary-into-another-dictionary-in-python else: print('Invalid selection.') From 656b3ab0987efe33372cb5d3d15b27df629009af Mon Sep 17 00:00:00 2001 From: navarroalan55 Date: Fri, 16 Dec 2016 17:51:47 -0500 Subject: [PATCH 6/8] COMPLETE --- library.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/library.py b/library.py index 9aff805..fbaae99 100644 --- a/library.py +++ b/library.py @@ -71,7 +71,7 @@ def check_out(filename, isbn, s_id): print("Book is not in Library") else: books[isbn]['checked_out'] = s_id - print('%s has been checked out' % isbn) + print("%s has been checked out" % isbn) # And again save the data here @@ -96,7 +96,7 @@ def return_book(filename, isbn): json.dump({'students': students, 'books': books}, f) -def status(filename, isbn, ): +def status(filename, isbn): students, books = open_library(filename) # Print out two lists - one of all books currently checked out, # and one of all available books. @@ -117,16 +117,20 @@ def status(filename, isbn, ): elif option == '2': print(" BOOKS THAT HAVE BEEN CHECKED OUT:") + print("") + + for key in books: + dict = books[key] + for thing in dict: + if thing == 'checked_out': + print(books[key]) + - if books[isbn]["check_out"]: - for key in books: - print(key, books[isbn]["check_out"]) - http://stackoverflow.com/questions/3817529/syntax-for-creating-a-dictionary-into-another-dictionary-in-python else: print('Invalid selection.') -# Main loofor key, value in mydic.iteritems() : + From e98de1d233eacd7e6caa1ae430b01dbb668c8d47 Mon Sep 17 00:00:00 2001 From: navarroalan55 Date: Mon, 19 Dec 2016 17:22:59 -0500 Subject: [PATCH 7/8] fixed checkout bug --- library.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/library.py b/library.py index fbaae99..8a1b65f 100644 --- a/library.py +++ b/library.py @@ -67,12 +67,20 @@ def check_out(filename, isbn, s_id): isbn = input('what book do you want to check out(choose by isbn)?') - if isbn not in books: - print("Book is not in Library") - else: + if books[isbn]: + dict=books[isbn] + for thing in dict: + if thing == 'checked_out': + print("Book has already been checked out, SORRY!!") + + + elif isbn in books: books[isbn]['checked_out'] = s_id print("%s has been checked out" % isbn) + else: + print("Book is not in Library") + # And again save the data here with open(filename, 'w') as f: From d7b2d1f94379f3041530846d73b57e72b6bb0166 Mon Sep 17 00:00:00 2001 From: navarroalan55 Date: Tue, 20 Dec 2016 11:21:12 -0500 Subject: [PATCH 8/8] Update library.py --- library.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/library.py b/library.py index 8a1b65f..c2a8fc8 100644 --- a/library.py +++ b/library.py @@ -63,24 +63,28 @@ def check_out(filename, isbn, s_id): print('') print('') - s_id = input("what is ur ID?") isbn = input('what book do you want to check out(choose by isbn)?') - if books[isbn]: - dict=books[isbn] + dict = books[isbn] for thing in dict: if thing == 'checked_out': print("Book has already been checked out, SORRY!!") - elif isbn in books: - books[isbn]['checked_out'] = s_id - print("%s has been checked out" % isbn) + elif isbn is not books: + print("book is not in library, go to add books") + + + else: + if isbn in books: + s_id = input("what is ur ID?") + books[isbn]['checked_out'] = s_id + print("%s has been checked out" % isbn) + + else: + print("Book is not in Library") - else: - print("Book is not in Library") - # And again save the data here with open(filename, 'w') as f: @@ -138,10 +142,6 @@ def status(filename, isbn): print('Invalid selection.') - - - - while True: print('=' * 21) print(' Library System') @@ -169,3 +169,9 @@ def status(filename, isbn): status('data/test.json', 'isbn', ) else: print('Invalid selection.') + + + + + +