CBSE Class 12 Computer Science Question 31 of 57

Review of Python Basics — Question 31

Back to all questions
31
Question

Question 26

Write a Python program to input names of 'n' customers and their details like items bought, cost and phone number, etc., store them in a dictionary and display all the details in a tabular form.

Solution
n = int(input("Enter the number of customers: "))
customer_data = {}

for i in range(n):
    name = input("Enter customer name: ")
    items_bought = input("Enter items bought: ")
    cost = float(input("Enter cost: "))
    phone_number = int(input("Enter phone number: "))

    customer_data[name] = {
        'Items Bought': items_bought,
        'Cost': cost,
        'Phone Number': phone_number
    }

print("Customer Details:")
print("Name\t\tItems Bought\t\tCost\t\tPhone Number")
for name, details in customer_data.items():
    print(name, "\t\t", details['Items Bought'], "\t\t", details['Cost'], "\t\t", details['Phone Number'])
Output
Enter the number of customers: 4
Enter customer name: Sakshi
Enter items bought: Chocolate, Icecream
Enter cost: 200
Enter phone number: 9876354678
Enter customer name: Shashank
Enter items bought: Biscuits, Maggie, Soup
Enter cost: 450
Enter phone number: 8976346378
Enter customer name: Saanvi
Enter items bought: Veggies, Coffee
Enter cost: 300
Enter phone number: 8794653923
Enter customer name: Shivank
Enter items bought: Door mat, Bottles, Boxes
Enter cost: 600
Enter phone number: 6737486827
Customer Details:
Name            Items Bought            Cost            Phone Number
Sakshi           Chocolate, Icecream             200.0           9876354678
Shashank                 Biscuits, Maggie, Soup                  450.0           8976346378
Saanvi           Veggies, Coffee                 300.0           8794653923
Shivank                  Door mat, Bottles, Boxes                600.0           6737486827
Answer

n
=
int
(
input
(
"Enter the number of customers: "
))
customer_data
=
{}
for
i
in
range
(
n
):
name
=
input
(
"Enter customer name: "
)
items_bought
=
input
(
"Enter items bought: "
)
cost
=
float
(
input
(
"Enter cost: "
))
phone_number
=
int
(
input
(
"Enter phone number: "
))
customer_data
[
name
]
=
{
'Items Bought'
:
items_bought
,
'Cost'
:
cost
,
'Phone Number'
:
phone_number
}
print
(
"Customer Details:"
)
print
(
"Name
\t
\t
Items Bought
\t
\t
Cost
\t
\t
Phone Number"
)
for
name
,
details
in
customer_data
.
items
():
print
(
name
,
"
\t
\t
"
,
details
[
'Items Bought'
],
"
\t
\t
"
,
details
[
'Cost'
],
"
\t
\t
"
,
details
[
'Phone Number'
])
Output
Enter the number of customers: 4
Enter customer name: Sakshi
Enter items bought: Chocolate, Icecream
Enter cost: 200
Enter phone number: 9876354678
Enter customer name: Shashank
Enter items bought: Biscuits, Maggie, Soup
Enter cost: 450
Enter phone number: 8976346378
Enter customer name: Saanvi
Enter items bought: Veggies, Coffee
Enter cost: 300
Enter phone number: 8794653923
Enter customer name: Shivank
Enter items bought: Door mat, Bottles, Boxes
Enter cost: 600
Enter phone number: 6737486827
Customer Details:
Name Items Bought Cost Phone Number
Sakshi Chocolate, Icecream 200.0 9876354678
Shashank Biscuits, Maggie, Soup 450.0 8976346378
Saanvi Veggies, Coffee 300.0 8794653923
Shivank Door mat, Bottles, Boxes 600.0 6737486827