tuples and their operations
Tuples are very similar to lists in many places and are ordered, and can be obtained by [] specifying an index, but the tuples cannot be changed after they are created.
names=(‘sheldon‘,‘penny‘,‘leonard‘)print(names)print(names[1]) # 切print(names.count(‘penny‘)) # 统计print(names.index(‘penny‘)) # 索引# names[1]=‘潘妮‘ # 替换 不可以# names.remove(‘penny‘) # 删 不可以# names.append(‘潘妮‘) # 增 不可以
Shopping Cart Example
1. Simple Shopping Cart
The shortcomings of the existence of once, the product number is wrong to run the failure;
salary=int(input(‘salary:‘))names1=(‘Iphone‘,‘Bike‘,‘Book‘,‘Car‘)names2=(‘2500‘,‘800‘,‘60‘,‘3000‘)names3=[]print(‘-------------------‘)for i in names1: num=names1.index(i ) price=names2[names1.index(i) ] print(num,i,price)print(‘-------------------‘)_abc=input(‘你是否要购物:(add)or(other)‘)while _abc==‘add‘: num2=int(input("The shopping num:")) price2=int(names2[num2]) if salary>price2: names3.append(names1[num2]) names3.append(names2[num2]) print(‘你刚刚购买了:‘,num2,names1[num2],names2[num2 ]) salary=salary-price2 print(‘-------------------‘) for i in names1: num = names1.index(i) price = names2[names1.index(i)] print(num, i, price) print(‘-------------------‘) _abc=input(‘你是否要继续购物:(add)or(other)‘) else: print("you money is not enougy") breakprint("你购买的商品:",names3)print(‘你的余额:‘,salary)
2. After the perfect procedure
product_list=[(' Iphone ', 5800), (' Mac Pro ', 9800), (' Bike ', 8000), (' book ', ' + '), (' Car ', "200"), (' food ') ] # Nested type shopping_list=[]salary=input (' Input you Salary: ') if Salary.isdigit (): Salary=int (Salary) While True:for Index,item in Enumerate (product_list): # Take out the list of subscript print (Index,item) use_choic E=input ("What you want to buy:") if Use_choice.isdigit (): Use_choice =int (use_choice) if Use_choice >=0 and Use_choice <len (product_list): p_item=product_list [Use_choice] if p_item[1]<=s Alary:shopping_list. Append (P_item) salary-=p_item[1] Print ("A Dded%s to shopping car,you Moneny is \033[31;1m%s\033[0m "% (P_item, salary)) Else:pri NT ("\033[41;1m your balance is insufficient, only left%s\033[0m"% (Salary)) Else:print ("No such product ...") elif Use_choice = = ' Q ': Print ("----------Shopping list-----------") for I in Shopping_list:print (i) print (' Your balance left%s ' % (Salary)) # Break exit () Else:print ("Invalid option")
Python's Learning note/002-4 (2018-5-20)