KnowledgeBoat Logo
LoginJOIN NOW

Computer Science

Write add(Books) and delete(Books) methods in Python to add Books and Remove Books considering them to act as append() and pop() operations in Stack.

Python

Python Stack

9 Likes

Answer

def add(Books):
    bookname = input("Enter the book name: ")
    Books.append(bookname)

def delete(Books):
    if Books == []:
        print("Books is empty")
    else:
        print("Deleted Book", Books.pop())
        
Books = []

add(Books)
add(Books)
add(Books)
print("Initial Books Stack:", Books)


delete(Books)
print("Books Stack after deletion:", Books)

Output

Enter the book name: Three thousand stitches
Enter the book name: Wings of fire
Enter the book name: Ignited minds
Initial Books Stack: ['Three thousand stitches', 'Wings of fire', 'Ignited minds']
Deleted Book Ignited minds
Books Stack after deletion: ['Three thousand stitches', 'Wings of fire']

Answered By

3 Likes


Related Questions