Jawaban:
Penjelasan:
import json
def input_book_record():
title = input("Enter the book title: ")
author = input("Enter the book author: ")
isbn = input("Enter the ISBN: ")
return {'title': title, 'author': author, 'isbn': isbn}
def display_book_record(books):
books = sorted(books, key=lambda x: x['title'])
for book in books:
print(f"Title: {book['title']}\nAuthor: {book['author']}\nISBN: {book['isbn']}\n")
def update_book_record(books):
isbn = input("Enter the ISBN of the book to update: ")
if book['isbn'] == isbn:
book['title'] = input("Enter the new title: ")
book['author'] = input("Enter the new author: ")
print("Book record updated.")
return
print("Book not found.")
def erase_book_record(books):
isbn = input("Enter the ISBN of the book to erase: ")
books.remove(book)
print("Book record erased.")
def save_and_exit(books):
with open('books.json', 'w') as f:
json.dump(books, f)
print("Data saved and program exited.")
def main():
books = []
while True:
print("1. Input book record\n2. Display book record\n3. Update book record\n4. Erase book record\n5. Save and Exit")
choice = input("Enter your selection: ")
if choice == '1':
books.append(input_book_record())
elif choice == '2':
display_book_record(books)
elif choice == '3':
update_book_record(books)
elif choice == '4':
erase_book_record(books)
elif choice == '5':
save_and_exit(books)
break
else:
print("Invalid selection.")
if __name__ == '__main__':
main()
" Life is not a problem to be solved but a reality to be experienced! "
© Copyright 2013 - 2024 KUDO.TIPS - All rights reserved.
Jawaban:
Penjelasan:
import json
def input_book_record():
title = input("Enter the book title: ")
author = input("Enter the book author: ")
isbn = input("Enter the ISBN: ")
return {'title': title, 'author': author, 'isbn': isbn}
def display_book_record(books):
books = sorted(books, key=lambda x: x['title'])
for book in books:
print(f"Title: {book['title']}\nAuthor: {book['author']}\nISBN: {book['isbn']}\n")
def update_book_record(books):
isbn = input("Enter the ISBN of the book to update: ")
for book in books:
if book['isbn'] == isbn:
book['title'] = input("Enter the new title: ")
book['author'] = input("Enter the new author: ")
print("Book record updated.")
return
print("Book not found.")
def erase_book_record(books):
isbn = input("Enter the ISBN of the book to erase: ")
for book in books:
if book['isbn'] == isbn:
books.remove(book)
print("Book record erased.")
return
print("Book not found.")
def save_and_exit(books):
with open('books.json', 'w') as f:
json.dump(books, f)
print("Data saved and program exited.")
def main():
books = []
while True:
print("1. Input book record\n2. Display book record\n3. Update book record\n4. Erase book record\n5. Save and Exit")
choice = input("Enter your selection: ")
if choice == '1':
books.append(input_book_record())
elif choice == '2':
display_book_record(books)
elif choice == '3':
update_book_record(books)
elif choice == '4':
erase_book_record(books)
elif choice == '5':
save_and_exit(books)
break
else:
print("Invalid selection.")
if __name__ == '__main__':
main()