简体   繁体   中英

adding a row of items to already existing list *PYTHON*

I'M trying to get the input of the user to a new book to my already existing list but finding it to do this. Below is the code and list I've been trying to use but not finding any luck. If anyone could help it would be very much appreciate. btw the list is originally txt file but I import it into python and put it into a empty list, so it is now a list of lists

def print_summary4():
    NBooks = []
    for index in range(master_book_list):
        author = str(input('Name of Author you wish to add: '))
        NBooks.append(author)
        title = str(input('Name of book you wish to add: '))
        NBooks.append(title)
        form = str(input('Format of book: '))
        NBooks.append(form)
        pub = str(input('Name of publisher: '))
        NBooks.append(pub)
        cost = int(input('What is the cost of this book: '))
        NBooks.append(cost)
        stock = int(input('How many stock does this book have: '))
        NBooks.append(stock)
        genre = str(input('What genre is this: '))
        NBooks.append(genre)
    NBooks()

list

#Listing showing sample book details 
#AUTHOR, TITLE, FORMAT, PUBLISHER, COST?, STOCK, GENRE
P.G. Wodehouse, Right Ho Jeeves, hb, Penguin, 10.99, 5, fiction
A. Pais, Subtle is the Lord, pb, OUP, 12.99, 2, biography
A. Calaprice, The Quotable Einstein, pb, PUP, 7.99, 6, science
M. Faraday, The Chemical History of a Candle, pb, Cherokee, 5.99, 1, science
C. Smith, Energy and Empire, hb, CUP, 60, 1, science
J. Herschel, Popular Lectures, hb, CUP, 25, 1, science
C.S. Lewis, The Screwtape Letters, pb, Fount, 6.99, 16, religion
J.R.R. Tolkein, The Hobbit, pb, Harper Collins, 7.99, 12, fiction
C.S. Lewis, The Four Loves, pb, Fount, 6.99, 7, religion
E. Heisenberg, Inner Exile, hb, Birkhauser, 24.95, 1, biography
G.G. Stokes, Natural Theology, hb, Black, 30, 1, religion
 

From your list, I am guessing that it is a list of lists. Hence I would advice you to add all the user inputs into a fresh list and then append this list to the list of books, almost similar to what you did. Here's is the code:

def print_summary4():
    NBooks = []
    for index in range(len(master_book_list)):
        author = str(input('Name of Author you wish to add: '))
        NBooks.append(author)
        title = str(input('Name of book you wish to add: '))
        NBooks.append(title)
        form = str(input('Format of book: '))
        NBooks.append(form)
        pub = str(input('Name of publisher: '))
        NBooks.append(pub)
        cost = int(input('What is the cost of this book: '))
        NBooks.append(cost)
        stock = int(input('How many stock does this book have: '))
        NBooks.append(stock)
        genre = str(input('What genre is this: '))
        NBooks.append(genre)
        master_book_list.append(NBooks)
    NBooks

I am adding the NBooks to the master_book_list after each iteration. Let me know if this sounds good.

Edit: If you want to add only 1 book, you can remove the for loop like this:

def print_summary4():
        NBooks = []
        author = str(input('Name of Author you wish to add: '))
        NBooks.append(author)
        title = str(input('Name of book you wish to add: '))
        NBooks.append(title)
        form = str(input('Format of book: '))
        NBooks.append(form)
        pub = str(input('Name of publisher: '))
        NBooks.append(pub)
        cost = int(input('What is the cost of this book: '))
        NBooks.append(cost)
        stock = int(input('How many stock does this book have: '))
        NBooks.append(stock)
        genre = str(input('What genre is this: '))
        NBooks.append(genre)
        master_book_list.append(NBooks)
        NBooks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM