Trying to lead list from txt file in a library system | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Trying to lead list from txt file in a library system

class Library: def __init__(self,listofbooks): #what attributes does a library in general have? - for now, let's abstract and just say it has availablebooks (we're not going to program the shelves, and walls in!) self.availablebooks=listofbooks def displayAvailablebooks(self): print("Here are the books in our library:") print("_________________________________") for book in self.availablebooks: print(book) def lendBook(self,requestedBook): if requestedBook in self.availablebooks: print("you have successfully borrowed the book") self.availablebooks.remove(requestedBook) else: print("Sorry the book you have requested is currently not in the library") def addBook(self,returnedBook): self.availablebooks.append(returnedBook) print("You have successfully returned the book. Thank You!") class Student: def requestBook(self): print("Enter the name of the book you would like to lend ") self.book=input() return self.book def returnBook(self): print("Enter the name of the book you would like to return ") self.book=input() return self.book def main(): f = open("books.txt", "w") for sublist in books: sublist = str(sublist) f.write(str(sublist[1:-1])) f.write("\n") f.close() library=Library() student=Student() done=False while done==False: print(""" ======WELCOME TO THE LIBRARY MENU======= Please Select One of the Four Options :) 1. Display all available books 2. Request a book 3. Return a book 4. Exit =========

22nd Jul 2020, 12:23 PM
Aaaawaw1010
Aaaawaw1010 - avatar
2 Respostas
+ 1
Save the people from hassle for copy pasting code portions by saving your code and share its link instead of raw text like this. Follow below guide to do it šŸ‘‡ https://www.sololearn.com/post/75089/?ref=app
22nd Jul 2020, 12:55 PM
Ipang
0
I have managed to get the code working when it is reading straight from a list with the following program def main(): library=Library(["Tales of Two Cities","Black House","Hound of the Baskervilles"]) student=Student() done=False while done==False: print(""" ======WELCOME TO THE LIBRARY MENU======= Please Select One of the Four Options :) 1. Display all available books 2. Request a book 3. Return a book 4. Exit ============ THANK YOU============ """) choice=int(input("Enter Choice:")) if choice==1: library.displayAvailablebooks() elif choice==2: library.lendBook(student.requestBook()) elif choice==3: library.addBook(student.returnBook()) elif choice==4: sys.exit() main() however, it will not read from a specific books.txt file
22nd Jul 2020, 12:27 PM
Aaaawaw1010
Aaaawaw1010 - avatar