Python Shopping Cart Implementation course

Source: Internet
Author: User

Demand:

1. User input Wage income

2. Print a list of items

3. The user selects the product, unceasingly joins the shopping cart

4. Checking the user balance, direct donation, insufficient indication of insufficient balance

5. Allow active exit, when exiting, Print list of purchased items

Key methods:

To print a list of subscript methods:

a=[' Alex ', ' sys ', ' root ', ' admin '

>>> for Index,i in Enumerate (a):
.. print (Index,i)

0 Alex
1 sys
2 root
3 admin

1 #!/usr/bin/env Python32 #-*-conding:utf-8-**3 #__author__: ' Liudong '4Salary=input ("Input Your Salary:")5 ifsalary.isdigit ():6salary=Int (Salary)7 Else:8Exit"Invalid data Type ...")9welcom_msg='Welcome to Shopping mall'. Center (50,'-')Ten Print(welcom_msg) Oneexit_flag=False AProduct_list = [ -('Iphone', 5000), -('Mac Air', 8000), the('Mac Pro', 9000), -('Xiaomi', 20), -('Coffe', 30), -('Bike', 800), +('Cloth', 200) - ] +Shop_car = [] A  while  notExit_flag: at     #For Product_item in product_list: -     #p_name,p_price = Product_item #可选的写法 -     Print("Products List". Center (50,'-')) -     #For P_name,p_price in product_list: -         #print (P_name,p_price) #由于此方法后面打印下标时, will turn into 2 no groups, so use the following method of printing -      forIteminchEnumerate (product_list): inindex=Item[0] -P_name=item[1][0] toP_price=item[1][1] +         Print(Index,'.', P_name,p_price) -User_choice = input ('[Q=quit,c=check]what does want to buy?:') the     ifUser_choice.isdigit ():#must be the choice of goods *User_choice =Int (user_choice) $         ifUser_choice <Len (product_list):Panax NotoginsengP_item =Product_list[user_choice] -             ifP_item[1] <= Salary:#buy the UP theShop_car.append (P_item)#Add to Cart +Salary-= p_item[1]#Deduct Money A                 Print('Added [%s] into your shop car,your current balance is \033[31;1m[%s]\033[0m'% the (p_item,salary)) # font plus color +             Else: -                 Print('Your balance is [%s],cannot afford this product:'%salary) $     Else: $         ifUser_choice = ='Q' orUser_choice = ='quit': -             Print('purchased products as blew:'. Center (40,'*')) -              forIteminchShop_car: the                 Print(item) -             Print('END'. Center (40,'*'))Wuyi             Print('Your balance is [%s]'%salary) the             Print('bye.') -Exit_flag =True Wu         elifUser_choice = ='C' orUser_choice = ='Check': -             Print('purchased products as blew:'. Center (40,'*')) About              forIteminchShop_car: $                 Print(item) -             Print('Your balance is \033[41;1m[%s]\033[0m'% salary)#Background plus color
View Code

The thinking of programming must also be strengthened .....

Python Shopping Cart Implementation course

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.