1. tuple
Features: CRUD can not only search, also known as read-only list
Two uses: count and index
names = ["han","wang","liu","hanjiali","han" ] print(names.index("wang"))
Output:
1
What tuple?
Information not want to be changed, use a tuple.
2. Shopping cart program
Requirements: 1. After starting the program, allowing users to enter wages, and then print the list of goods
2. allow users to purchase by product number
3. When the user selects a product, testing the balance is enough, enough to direct deduction, enough to remind
4. can withdraw at any time, when you exit, print the purchase of goods and balances
Shopping class (): DEF __init __ (Self): self.list1 = [] self.a the INPUT = ( "Please enter your wage:") self.start () DEF Start (Self): self.list = [ "1 Apple phone: 8000 "," 2 Huawei phone: 10900 yuan "," 3 Men's T sleeve: 500 yuan, "" 4 Men's shorts: 300 yuan "] Print (self.list) self.shop () Shop DEF (Self): self.b the iNPUT = ( "Please enter the article number (1,2,3,4) you want to buy:") self.list2 = [ '1', '2', '3' , '4'] iF self.b in self.list2: self.list1.append (self.b) self.c = int (the INPUT ( "do we have selected commodities (1. or 2. are not a):" )) IF self.c == 1: Self.shop() else: self.chack() the else: Print ( "Your input is incorrect, please re-enter:") self.shop () DEF CHACK (Self): Print ( "you want to purchase commodities S%"% self.list1) self.d = int (input ( "Please make sure is correct (1 or 2. correct incorrect)?.")) iF self.d == 2: self.shop () the else: self.money () DEF Money (Self): NUM = 0 for i in self.list1: IF i == '1': NUM + = 8000 Print ( "iPhone") IF i == '2': NUM = 10900 + Print ( "Huawei cell phone") IF i == '3': + = 500 NUM Print ( "Men T sleeve") i == IF '4': + 300 = NUM Print ( "boxer shorts") Print ( "something you purchased a total of% d dollars"% NUM) IF int (self.a) <NUM: Print (. "I'm sorry, you are out of balance") the else : self.m = self.a - NUM Print ( "buy success, your balance is:% d"% self.m) IF __name__ == '__main__': Shopping = Shopping ()
operation result:
C: \ Users \ Han Jiali \ Desktop \ Old Boys \ python \ 2019.8 \ 001 \ venv \ Scripts \ python.exe C: / Users / Hanjia Li / Desktop / Old Boys /python/2019.8/001/01.py enter your salary: 8000 [ '1. Apple phone: 8000' '2. Huawei phone: 10900 yuan', '3. Men's T sleeve: 500 yuan,' '4. Men's shorts: 300 yuan'] Please enter your want to buy goods number (1,2,3,4): 1 whether to also select merchandise (1. or 2. are not a): 1 Please enter the article number (1,2,3 you want to buy, 4): 3 whether to also select merchandise (1. or 2. are not the): 2 you want to purchase goods are [ '1', '3'] Make sure that correct? (1. or 2. correct incorrect) 1 Apple phone Men's T-cuff stuff you purchased a total of 8500 yuan Sorry, your balance is. Please enter your wages:
Please enter your salary: 10000 [ '1. Apple phone: 8000' '2. Huawei phone: 10900 yuan', '3. Men's T sleeve: 500 yuan,' '4. Men's shorts: 300 yuan'] please enter the product number you want to buy (1,2,3,4): 1 whether to also select merchandise (1. or 2. are not a): 1 Please enter the article number (1, 2, you want to buy, 3,4): 2 whether to also select merchandise (1. or 2. are not the): 2 you want to purchase goods are [ '1', '2'] Please make sure is correct? (1. or 2. correct incorrect) 1 Apple phone Huawei cell phone stuff you purchased a total of 18,900 yuan Sorry, your balance is. Please enter your wages: