Programming Examples

Python program to store the product information in dictionary


WAP that repeatedly asks the user to enter product names and prices. Store all of them in a dictionary whose keys are product names and values are prices. And also write a code to search an item from the dictionary.

Solution

dict={}
ch='y'
while ch=='y' or ch=='Y':
    name=input("Enter name of product : ")
    price=eval(input("Enter the price of product : "))
    dict[name]=price
    ch=input("Want to add more items (Y/N) : ")
print(dict)
nm=input("Enter the product you want to search : ")
for x in dict:
    if x==nm:
        print("Product found and the price of product ",x," is ",dict[x])
Output/ Explanation:

Enter name of product : pen
Enter the price of product : 45
Want to add more items (Y/N) : y
Enter name of product : notebook
Enter the price of product : 40
Want to add more items (Y/N) : y
Enter name of product : pencil
Enter the price of product : 10
Want to add more items (Y/N) : n
{'pen': 45, 'notebook': 40, 'pencil': 10}
Enter the product you want to search : notebook
Product found and the price of product  notebook  is  40
Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad