12. Output Product list, user input serial number, display user selected item (upgrade question)
Product List:
Goods = [{"Name": "Computer", "Price": 1999},
{"Name": "Mouse", "Price": 10},
{"Name": "Yacht", "Price": 20},
{"Name": "Beauty", "Price": 998},]
Requirements:
1: Page display serial number + product name + commodity price, such as:
1 PC 1999
2 Mouse 10
...
2: The user enters the selected product serial number, then prints the product name and the commodity price
3: If the user entered an incorrect serial number, you are prompted to enter it incorrectly and re-enter it.
4: User input Q or q, exit the program.
LST =[]shopcars= {} while1: Goods= [{"name":"Computer"," Price": 1999}, {"name":"Mouse"," Price": 10}, {"name":"Yacht"," Price": 20}, {"name":"Beauty"," Price": 998}, ] forI,jinchEnumerate (goods,1): Print(i,j['name'],j[' Price']) Lst.append (j) Choice= Input ('Please enter a product number to select a product:') ifchoice.isdigit (): Choice=int (choice) PName= lst[choice-1]['name'] Pprice= lst[choice-1][' Price'] Print(Pname,pprice)ifPNameinchshopcars:shopcars[pname]['Amount'] +=1Else: Shopcars[pname]= {'Pprice': Pprice,'Amount': 1} elifChoice.upper () = ='Q': Print(Shopcars) Break Else: Print('product serial number is wrong!')
Python3.6 full Stack Development example [012]