Problem 1 (Defining products and Creating a Menu)
Write a program called a2_p1.py that asks a shop owner for product information and creates a menu system that allows customers to purchase multiple products of varying quantities. The program should start by asking the shop owner’s name (string) and company name (string). Next it should ask for three (3) products (strings), their prices (int – not ideal, but for now we will keep it this way), and the number of products in stock (int). Finally, it should display a menu.
It is possible that the user will enter any type of data, including text, floating point numbers, negative numbers, etc.. Invalid selections should be handled with a suitable error messages and the menu should be repeated. Below is a sample run of the program that demonstrates how your output could look (user input is highlighted for emphasis): Sample output: Please enter your name: Alice What is your company name? Wonderland Hi Alice! Let's setup a sales menu for 'Wonderland'. Please enter the following information for Wonderland's products. Enter Product 1 information: Name: Cake Cake's Price: two Price must be a number, please enter again. Cake's Price: -2 Price must be a number, please enter again. Cake's Price: 2 How many Cake in stock: ten Enter again, stock must be a positive number. How many Cake in stock: -10 Enter again, stock must be a positive number. How many Cake in stock: 10 Enter Product 2 information: Name: Hat Hat's Price: 3 How many Hat in stock: 7 Enter Product 3 information: Name: Bunny Bunny's Price: 8 How many Bunny in stock: three each Enter again, stock must be a positive number. How many Bunny in stock: 3 Your menu: =================================== Please select the item you want to buy from the following menu: 1. Cake (each $2.00), 10 in stock 2. Hat (each $3.00), 7 in stock 3. Bunny (each $8.00), 3 in stock Press 4 when you are done! ===================================
Short Summary:
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
class a2_p1(object):
def validateAndDisplayMenu(self,num): #function to prompt user and
validate input
name = input("Please enter your name: ") #Ask user for name
companyName = input("What is your company name? ") #Ask user for
company name
print("Hi ",name,"!","Let's setup a sales menu for
'",companyName,"'.")
print("Please enter the following information for ",companyName,"'s
products.")
prodList = [None] * num #create product list of product size
priceList= [None] * num #create price list of product size
stockList= [None] * num #create stock list of product size
for i in range(num): #loop number of product times
var = "Enter Product " +str(i+1) + " information: Name: "
prodName = input(var)
prodList[i]=prodName
valid=False
while valid==False: #loop until valid price is entered
var=prodName+"'s price:"
price=input(var)
if price.isnumeric() and int(price) > 0:
priceList[i]=price
valid=True
else:
print("Price must be a number, please enter again.")
valid=False
valid=False #reassign valid to false
while valid==False: #loop until valid stock is entered
var="How many "+prodName+" in stock:"
stock=input(var)
if stock.isnumeric() and int(stock) > 0:
stockList[i]=stock
valid=True
else:
print("Enter again.Stock must be a positive number")
valid=False
print("Your menu: =================================== Please select
the item you want to buy from the following menu:")
for i in range(num):
print(i,". ",prodList[i],"(each $",priceList[i],")", stockList[i],"
in stock")
print(" Press 4 when you are done!
===================================")
if __name__ == "__main__":
product=3
a2_p1().validateAndDisplayMenu(product)
Code Screenshot:
Output:
**************Please do upvote to appreciate our time. Thank you!******************
Get Answers For Free
Most questions answered within 1 hours.