Programming Examples

Python program that repeatedly asks the user to enter product names and prices


Write a program that repeatedly asks the user to enter product names and prices. Store all of these  in a dictionary whose keys are the product names and whose values are the price .

When the user is done entering products and price, allow them to repeatedly enter a product name and print the corresponding price or a message if the product is not in dictionary.

Solution

dic = { }
while True :
    product = input("enter the name of product (enter q for quit )= ")
    if product == "q" or product == "Q" :
        break
    else :
        price = int(input("enter the price of product = "))
        dic [ product ] = price
while True :
    name = input("enter the name of product those you are entered (enter q for quit )= ")
    if name == "q" or name == "Q" :
        break
    else :
        if name not in dic :
            print("name of product is invaild")
        else :
            print("price of product = ",dic[name])
Output

CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad