Advertisement
nein_yards

Library 1

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