def lendbook(): bn = input(f'Which book you want?\n') with open('book.txt', 'a') as…
- User
def lend_book(): bn = input(f'Which book you want?\n') with open('book.txt', 'a') as file: lines = file.readlines() bl = [] for line in lines: if bn in line: parts = line.strip().split('; ') if parts[3] == 1: file.write(f"{parts[0]}; {parts[1]}; {parts[2]}; {0}\n") else: print('The book is being lend!!!')
codeif len(bl) == 0: print("No this book here!") else: print("Book information:") for line in bkinf: print(line.strip()) print('Oh Yeah!!!Success!!!')lend_book()
- Assistant
It looks like you're trying to write a function to lend a book from a text file that stores book information. Your code has several issues that need to be fixed for it to work correctly:
- Opening the file in append mode ('a') will not allow you to read the contents properly. Also, to update the book lending status correctly, you need to read all lines first, make modifications, and then write all lines back to the file.