Python Shopping Cart Program

Source: Internet
Author: User

Shopping Cart program writing method;

1, first of all its products listed, and then in the establishment of an empty list, for storing the goods purchased

2, enter the user's wages, in the judge whether the input is a number, if not, quit if it is to continue to execute

3. Enter into a dead loop while True:

4, the listing of all goods, in through the enumerate in the subscript out

5, let the user enter the number of products to be purchased, and determine whether the user input is a number

6, again determine whether the Q exit, if q is printed on the purchase of goods, and show the balance, if not the print prompt input error (invaild option), and re-enter

7, if the user is selected as a number, in judging whether the number in 0 and the listed goods through len() Judge

8, if in the range of the selected goods, in the determination of wages, check whether the purchase of the start, if you can purchase, add it by append and add it to the empty list

9, has been circulating to the user input q or not enough until the balance

and then print it out to

The procedure is as follows:

#首先通过列表将产品进行列出
Product_list = [
("iphone", 5800),
("MOC Pro", 9800),
("Bike", 800),
("Coffee", 31),
("Linux book", 80)
]
Shopping_list =[]
Salary = input ("Input your Salary:")
ifSalary.isdigit ():# used to determine if the input string is a number form true if yes
    salary = Int (salary)
While True:
# for item in Product_list: #for item in range product_list: wrong spelling
# Way One: print (Product_list.index (item), item) #通过 Index to display its subscript for the product number
         forIndex,iteminchEnumerate (product_list):# Method Two: index indicates that the subscript item represents the list data in the enumerate
            Print (Index,item)
User_choice = input ("Please input your want to buy thing:")
ifUser_choice.isdigit ():
User_choice =int (User_choice)
ifUser_choice<len (Product_list) andUser_choice>=0:
P_item =product_list[user_choice]#将用户选择的商品的下标取出来
                ifP_ITEM[1] <=salary:# Compare User-selected items with the salary of the user less than the salary indicated buy
                    Shopping_list.append (P_item)
Salary-=p_item[1]
Print"added%s into shopping cart,your balance\033[31;1m%s\033[0m]% (p_item,salary))#%s placeholder when used, it cannot be separated by commas , \033[31;1m%s\033[0m indicates that the value represented by the last placeholder is set to color
                Else:
Print"\033[41;1m Your balance remains%s, you can't afford it.\033[0m]%salary)
Else:
Print"Product [%s] is not exist!"%user_choice)
elifUser_choice = =' Q ':
Print"-------------shopping list-----------")
forPinchShopping_list:
Print (P)
Print"Your current balance:", salary)
Exit ()
Else:
Print"invaild option ...")

Python Shopping Cart Program

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.