CBSE Class 12 Computer Science Question 25 of 26

Data Structures in Python — Question 26

Back to all questions
26
Question

Question 25

Write AddCustomer(Customer) and DeleteCustomer(Customer) methods in Python to add a new Customer and delete a Customer from a List of CustomerNames, considering them to act as push and pop operations of the Stack data structure.

Solution
def AddCustomer(Customer):
    Customer_name = input("Enter the customer name: ")
    Customer.append(Customer_name)

def DeleteCustomer(Customer):
    if Customer == []:
        print("Customer list is empty")
    else: 
        print("Deleted Customer name:", Customer.pop())

Customer = []

AddCustomer(Customer)
AddCustomer(Customer)
AddCustomer(Customer)
print("Initial Customer Stack:", Customer)

DeleteCustomer(Customer)
print("Customer Stack after deletion:", Customer)
Output
Enter the customer name: Anusha
Enter the customer name: Sandeep
Enter the customer name: Prithvi
Initial Customer Stack: ['Anusha', 'Sandeep', 'Prithvi']
Deleted Customer name: Prithvi
Customer Stack after deletion: ['Anusha', 'Sandeep']
Answer

def
AddCustomer
(
Customer
):
Customer_name
=
input
(
"Enter the customer name: "
)
Customer
.
append
(
Customer_name
)
def
DeleteCustomer
(
Customer
):
if
Customer
==
[]:
print
(
"Customer list is empty"
)
else
:
print
(
"Deleted Customer name:"
,
Customer
.
pop
())
Customer
=
[]
AddCustomer
(
Customer
)
AddCustomer
(
Customer
)
AddCustomer
(
Customer
)
print
(
"Initial Customer Stack:"
,
Customer
)
DeleteCustomer
(
Customer
)
print
(
"Customer Stack after deletion:"
,
Customer
)
Output
Enter the customer name: Anusha
Enter the customer name: Sandeep
Enter the customer name: Prithvi
Initial Customer Stack: ['Anusha', 'Sandeep', 'Prithvi']
Deleted Customer name: Prithvi
Customer Stack after deletion: ['Anusha', 'Sandeep']