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/ Explanation:

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