Program: Shopping Cart Program
Demand:
- After you start the program, let the user enter the payroll, and then print the list of items
- Allow users to purchase items based on their product number
- After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
- 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