Advertisement
nein_yards

librarytextfile

Jan 12th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. # 2 procedures: NewBook, AuthorSearch
  2. def clear():
  3. print('\n' * 80)
  4.  
  5.  
  6. def new_book():
  7. clear()
  8. book_title = input("Enter book title: ")
  9. author_name = input("Enter the author's name: ")
  10. publisher_name = input("Enter the publisher's name: ")
  11. while True:
  12. try:
  13. number_pages = int(input("Enter the total number of pages: "))
  14. except ValueError:
  15. print("Please enter an integer value.")
  16. continue
  17. if number_pages < 0:
  18. print("Sorry, your response must not be negative.")
  19. continue
  20. else:
  21. break
  22. while True:
  23. try:
  24. number_copies = int(input("Enter the total number of copies: "))
  25. except ValueError:
  26. print("Please enter an integer value.")
  27. continue
  28. if number_copies < 0:
  29. print("Sorry, your response must not be negative.")
  30. continue
  31. else:
  32. break
  33. while True:
  34. try:
  35. is_paperback = bool(input("Is the book paperback?(True/False): "))
  36. except ValueError:
  37. print("Please enter either True or False.")
  38. continue
  39. else:
  40. break
  41. book_string = book_title + "," \
  42. + author_name + "," \
  43. + publisher_name \
  44. + "," + str(number_pages) + "," \
  45. + str(number_copies) \
  46. + "," + str(is_paperback)
  47. f = open("C:\\Users\\tfqsy\\OneDrive\\Desktop\\library.txt", "a+")
  48. f.write(book_string)
  49. f.close()
  50.  
  51.  
  52. def author_search(author_name):
  53. f = open("C:\\Users\\tfqsy\\OneDrive\\Desktop\\library.txt", "r")
  54. for book_string in f:
  55. book_info = book_string.split(",")
  56. if (book_info[1].lower() == author_name.lower()):
  57. print("Title: " & book_info[0])
  58. print("Author: " & book_info[1])
  59. print("Publisher: " & book_info[2])
  60. print("Number of Pages: " & book_info[3])
  61. print("Number of Copies: " & book_info[4])
  62. print("Paperback?: " & book_info[5])
  63. author_found = True
  64. if (not author_found):
  65. print("Author not found")
  66.  
  67.  
  68. while (True):
  69. clear()
  70. print("1 - Add new book to the text file")
  71. print("2 - Search for a book through author name")
  72. print("3 - Close Menu\n")
  73. menu_choice = int(input("Enter your choice (1 - 3): "))
  74.  
  75. clear()
  76. if (menu_choice == 1):
  77. new_book()
  78. elif (menu_choice == 2):
  79. author_name = input("Enter the author's name: ")
  80. author_search(author_name)
  81. elif (menu_choice == 3):
  82. break
  83. else:
  84. print("Invalid choice")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement