Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def clear():
- print('\n' * 100)
- def new_book():
- clear()
- book_title = input("Enter book title: ")
- author_name = input("Enter the author's name: ")
- publisher_name = input("Enter the publisher's name: ")
- while True:
- try:
- number_pages = int(input("Enter the total number of pages: "))
- except ValueError:
- print("Please enter an integer value.")
- continue
- if number_pages < 0:
- print("Sorry, your response must not be negative.")
- continue
- else:
- break
- while True:
- try:
- number_copies = int(input("Enter the total number of copies: "))
- except ValueError:
- print("Please enter an integer value.")
- continue
- if number_copies < 0:
- print("Sorry, your response must not be negative.")
- continue
- else:
- break
- while True:
- try:
- is_paperback = bool(input("Is the book paperback?(True/False): "))
- except ValueError:
- print("Please enter either True or False.")
- continue
- else:
- break
- book_string = book_title + "," \
- + author_name + "," \
- + publisher_name \
- + "," + str(number_pages) + "," \
- + str(number_copies) \
- + "," + str(is_paperback)
- f = open("C:\\Users\\tfqsy\\OneDrive\\Desktop\\library.txt", "a+")
- f.write(book_string)
- f.close()
- print("Book added!")
- input()
- def author_search(author_name):
- f = open("C:\\Users\\tfqsy\\OneDrive\\Desktop\\library.txt", "r")
- author_found = False
- for book_string in f:
- book_info = book_string.split(',')
- if (book_info[1].lower() == author_name.lower()):
- print("Title: " + book_info[0])
- print("Author: " + book_info[1])
- print("Publisher: " + book_info[2])
- print("Number of Pages: " + book_info[3])
- print("Number of Copies: " + book_info[4])
- print("Paperback?: " + book_info[5])
- author_found = True
- if (not author_found):
- print("Author not found")
- input()
- while (True):
- clear()
- print("1 - Add new book to the text file")
- print("2 - Search for a book through author name")
- print("3 - Close Menu\n")
- menu_choice = int(input("Enter your choice (1 - 3): "))
- clear()
- if (menu_choice == 1):
- new_book()
- elif (menu_choice == 2):
- author_name = input("Enter the author's name: ")
- author_search(author_name)
- elif (menu_choice == 3):
- break
- else:
- print("Invalid choice")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement