library = ['Harry Potter','Lord of the rings','Lupin']
user_input=input('Choose option: ')
# When adding a book , it is not saved in library, it only prints out what was by default.
How to save books in Library?
if user_input == 'add_book':
x = input('add_book: ')
library.append(x)
elif user_input =='show_books':
for e in library:
print((e),end=' ')
# How to specify which book to remove?
elif user_input =='remove':
library.pop()
Where to place details for each book? Details should appear when book is selected.
CodePudding user response:
This code resolves the problems you stated in your comments, the following points explain the changes.
library = ['Harry Potter','Lord of the rings','Lupin']
while True:
user_input = input('\n Choose option: ')
if user_input == 'add_book':
to_add = input('add_book: ')
library.append(to_add)
elif user_input =='show_books':
for e in library:
print((e),end=' ')
elif user_input =='remove_book':
to_remove = input("remove_book: ")
library.remove(to_remove)
elif user_input == 'quit':
break
else:
print("Available options: add_book, show_books, remove_book, quit")
While True
lets your entire algorithm start again untiluser_input == 'quit'
, when thebreak
command tells the program to exit theWhile True
loop, ending the program. This was necessary to solve the problem you described of the program not saving changes: the reason was your program ended after your first input, and when you start the program again, it only runs its source code, so it couldn't show any "changes" to variables you made using it previously. Now until you typequit
it "saves" every change you make.- I used
library.remove()
because it accepts the library key value as an argument.library.pop()
required the index instead. (Now you just need to enter the item (book) name.
( I couldn't implement the last function you described)
Where to place details for each book? Details should appear when book is selected.
You didn't describe the concept of "selecting" a book and what are the "details" of one.
Hope I've been of some help.
CodePudding user response:
I've figured out how to implement logic for displaying details about selected book.
elif user_input == 'book_details':
which_book = input('Which book: ')
if which_book == 'Harry Potter':
print('\n''Author: J. K. Rowling')
print('ISBN: 9780747532743')
print('Year of release: 1997')
if which_book == 'Lupin':
print('\n''Author: Maurice Leblanc')
print('ISBN: 9780141929828')
print('Year of release: 1907')
if which_book == 'Lord of the rings':
print('\n''Author: J. R. R. Tolkien')
print('ISBN: 9788845292613')
print('Year of release: 1954')