Python Simple Shopping cart Practice

Source: Internet
Author: User

Product_list = [(' iphone ', 5800), (' iwatch ', 2400), (' book ', +), (' Apple ')] #创建列表库
Shoppinglist = [] #定义购物车的列表
Salary = input ("Input your Salary:")
If Salary.isdigit (): #判断输入的salary是否为数字
salary = Int (salary) #声明为整型
While True: #当为真
For Index,item in Enumerate (product_list): #获取列表里每个元组的数字排列值
# Print (Product_list.index (item), item) #打印列表中对应商品排序
Print (Index,item)
User_choice = Input ("Select the item to be purchased:")
If User_choice.isdigit ():
user_choice = Int (user_choice)
If User_choice < Len (product_list) and User_choice >=0: #判断选择的商品编号是否在列表库中
Price_item = Product_list[user_choice] #单品的价格对应的编号
Print (Price_item)
Print (price_item[1]) #单品价格
If price_item[1] <= salary: #单品价格小于用户余额
Shoppinglist.append (Price_item) #购物车添加对应的商品编号
Salary-= price_item[1] #salary minus the price of the purchased item number
Print ("added \033[31;1m%s\033[0m into shopping cart, your balance is \033[31;1m%s\033[0m"% (price_item,salary)) # Print out cart and balance
Else
Print ("\033[41;1myour current isn't enough,%s\033[0m"%salary) #购买的现金不足支付 and prints out the balance corresponding to if User_choice < Len (product_list ) and User_choice >=0:
Else
Print ("Product code [%s] is not exist"% User_choice) #打印出不存在商品列表的
elif User_choice = = ' Q ': Whether the #接user_choice is a number, if q executes the following corresponding if User_choice.isdigit (): Also or
Print ("----------shopping list----------")
For P in Shoppinglist: #循环打印购物车
Print (P)
Print ("Your current balance:", salary) #打印当前余额
Exit () #退出
Else
Print ("Invalid option") # Invalid input corresponds to if User_choice.isdigit ():

#elif salary is not
#print ("Welcome to buy Buy sale")

Python Simple Shopping cart Practice

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.