Python Shopping Cart

Source: Internet
Author: User

Product_list = [
(' iphone ', 8000),
(' Mac Pro ', 9800),
("Bike", 3000),
("Watch", 10000),
("Cofee", 30),
("book", 100),
] #定义商品列表
Shoppint_list = [] #购物车空列表
While True:
Salary = input ("Input you Salary:") #输入预算
If Salary.isdigit (): #isdigit integer
salary = Int (salary)
While True: #循环
# for item in Product_list:
# Print (Product_list.index (item), item)
For Index,item in Enumerate (product_list): #下标做产品编号, enumerate out list subscript
Print (Index,item) #index下标, item data
User_choice = input ("Choose what to Buy >>>:") #用户输入
If User_choice.isdigit (): #整数编码
user_choice = Int (user_choice)
If User_choice < Len (product_list) and User_choice >-0: #商品取值范围
P_item = Product_list[user_choice]
If p_item[1] <= salary:
Shoppint_list.append (P_item)
Salary-= p_item[1]
Print ("Added%s into shopping cart,your current balance is%s"% (p_item,salary))
Else
Print ("\033[41;1m your balance remains [%s], and buy a chicken leg \033[0m"% (Salary)) # "\033[[\033[om" plus bright color
Else
Print ("Product does not exist")
elif User_choice = = ' Q ':
Print ("----Product list------")
For P in Shoppint_list:
Print (P)
Print ("You balance:", salary)
Exit ()
Else
Print ("Invalid option")
Else
Print ("Please enter a number")

Python Shopping Cart

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.