Python Learning-Shopping Cart Program

Source: Internet
Author: User

Program: Shopping Cart Program

Demand:

    1. After you start the program, let the user enter the payroll, and then print the list of items
    2. Allow users to purchase items based on their product number
    3. After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
    4. You can exit at any time to print the purchased goods and balances when exiting

The procedure is as follows:

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 #@Time: 2018/4/28 16:234 #@Author: Yang5 #@File: shopping_cart_program01.py6 #@Software: Pycharm7 #Define a product list8Product_list = [('Iphone', 6000),9('MAC Pro', 9800),Ten('Bike', 800), One('Watch', 10600), A('Coffee', 31), -('Alex python', 120),] -Shopping_list = []#define an empty shopping cart list the #Enter Payroll -Salary = input ('Input Your Salary:') - ifsalary.isdigit (): -Salary =Int (Salary) +     #cycle through the item number you want to buy -      whileTrue: +         #The enumerate () function is used to combine a data object that can be traversed (such as a list, tuple, or string) into an index sequence A          forIndex,iteminchEnumerate (product_list): at             Print(Index,item)#print out a numbered list of items -User_choice = input ('Select the product number to buy >>>:')#Enter the product number to be purchased -         ifUser_choice.isdigit () ==false andUser_choice.upper ()! ='Q': -             Print('\033[1;43m you entered the product number is not legal! \033[0m') - exit () -         elifUser_choice.isdigit () ==true:#determines whether the input string is made up of numbers inUser_choice =Int (user_choice) -             ifUser_choice < Len (product_list) andUser_choice >=0: toP_item =Product_list[user_choice] +                 ifP_item[1] <= Salary:#can afford - shopping_list.append (P_item) theSalary-= P_item[1] *                     Print('Add Item%s to cart, Remaining amount \033[1;31;42m%s\033[0m'%(p_item,salary)) $                     #highlighting: The first part: \033[display mode; foreground color; m + END part: \033[0mPanax Notoginseng                 Else:#can't afford to buy -                     Print('\033[1;41m your balance remains [%s], and buy a sweater! \033[0m'%salary) the             Else: +                 Print('\033[1;41m Product%s does not exist! \033[0m'%User_choice) A  the         #Exit Shopping Cart Program: Print out shopping list and balance +         elifUser_choice.upper () = ='Q': -             Print('---------------Shopping list---------') $              forPinchshopping_list: $                 Print(P) -             Print('you have the remaining balance:', salary) - exit () the         Else: - exit ()Wuyi Else:#if the entered wage is illegal, exit the program the     Print('The salary you entered is not legal! ') -Exit ()

Note: program reference old boy Alex, attached blog address: http://www.cnblogs.com/alex3714/articles/5717620.html

Python Learning-Shopping Cart Program

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.