In: Computer Science
Write out code for a nested if statement that allows a user to enter in a product name, store the product into a variable called product name and checks to see if that product exists in your nested if statement. You must include 5 product names to search for. If it is then assign the price of the item to a variable called amount and then print the product name and the cost of the product to the console. If it does not find any of the items in the nested if statement, then print that item cannot be found. (using Python Only)
SOLUTION:
Dear Student, I am pasting a code of above your query and i am also commenting on the code for your understading. And also attaching the snapshots of program and it's outputs.
PROGRAM:
# take an input of prduct name and store to product_name
variable
product_name = input("Enter product name : ")
# first if condition in this, assign 5 diffent products
if ( product_name=="pen" or product_name == "book" or product_name
== "box" or product_name == "pencil" or product_name ==
"eraser"):
# In the second if condition, assign the product cost
if(product_name == "pen"): #this is for pen
amount = 10
print("The product name is ", product_name ,"and it's cost is ",
amount , "rupees")
if(product_name == "book"): #this is for book
amount = 100
print("The product name is ", product_name ,"and it's cost is ",
amount , "rupees")
if(product_name == "box"): #this is for box
amount = 150
print("The product name is ", product_name ,"and it's cost is ",
amount , "rupees")
if(product_name == "pencil"): #this is for pencil
amount = 5
print("The product name is ", product_name ,"and it's cost is ",
amount , "rupees")
if(product_name == "eraser"): #this is for eraser
amount = 8
print("The product name is ", product_name ,"and it's cost is ",
amount , "rupees")
else: # If item is not found then print the below message
print("That item cannot be found!")
SNAPSHOTS:
OUTPUT 1:
OUTPUT 2:
OUTPUT 3:
OUTPUT 4: