name = "Gaowang" pwd = "123.ABC" list_he=[] #定义空列表, after receiving For i in range (3): username = input (" Please enter your account number: ") password = input (" Please enter your password: ") if username == name and password == pwd: #判断帐号与密码是否符合 print ("Congratulations,%s, login successful, welcome" % username) offer = int (Input ("Enter your stored value card amount:")) print (" ------------------------------------ -- Welcome to the shopping mall -- ------ ------------------------------ ") #格式化输出 print (" 1: Get Product List &NBSP;&NBSP;&NBSP;2: Recharge Your Shopping card 3: Exit system ") #格式化输出 gong_neng = int (Input ("Please select your feature")) if gong_neng == 1: # Enter the purchase goods link while True: shipin2 = [[' milk ', 20], [' jerky ', 30], [' Rice ', 15], [' bread ', 15], [' beer ', 3.5]] for i, a in enumerate (shipin2, 1): #循环打印商品列表 print ("Ordinal:%s" % i, "Product:%s" % a[0], "Price:%s Yuan" % a[1]) huo_qu = int ( Input ("Please enter the item you want to buy, enter [0] exit")) if huo_qu > 0 and huo_qu <= len (shipin2): #验证输入是否正确 j=shipin2[huo_qu-1] #购买的商品和价格 &nBsp; if j[1]>offer : #判断想要购买的商品价格是否超过了余额 print ("Your balance is insufficient, please recharge in time") else: offer = offer - j[ 1] #算出购买商品后的价格 print ("The item you purchased is %s " % j[0], " remainingAmount is%s " % offer) #输出购买的商品 list_he.append (j[0]) #把已购买商品添加至集合中 print ("You have purchased%s"%list_he) #已购买商品集合 elif huo_ qu==0: print ("Exit program, Goodbye") for m in list_he : print ("You have purchased%s"%m) break else: print ("Mall cargo temporarily short, please enter the correct product serial number") elif gong_ neng == 2: # Prepaid Shopping Card function chongzhi=int (Input ("Please enter the amount you want to recharge")) if chongzhi>0: offer+=chongzhi print ("RechargeSuccessful, current balance is%s "%offer) else: print ("Reload failed") break elif gong_neng==3: # Exit System print ("Exit system success") break else: print ("The account or password is not entered correctly, Please re-enter ") Else: print (" Try to run the program again if the user name or password exceeds the maximum number of times ")
Python Shopping cart feature implementation