python 實現購物車的優化
阿新 • • 發佈:2018-08-07
NPU .get int app 大於 選擇 enume 優化 根據
功能要求:
要求用戶輸入自己擁有總資產,例如:2000
顯示商品列表,讓用戶根據序號選擇商品,加入購物車
購買,如果商品總額大於總資產,提示賬戶余額不足,否則,購買成功。
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標", "price": 10},
{"name": "遊艇", "price": 20},
{"name": "美女", "price": 998},
]‘‘‘
goods = [ {"name": "電腦", "price": 1999}, {"name": "鼠標", "price": 10}, {"name": "遊艇", "price": 20}, {"name": "美女", "price": 998}, ] while True: name =input("輸入用戶名:") mima =input("請輸入用戶密碼:") if name =="qi"and mima =="123": print("登陸成功") break else: print("用戶名或密碼錯誤") money = int(input("總資產")) lst=[] while 1: print(‘----------商品列表------------‘) for i,m in enumerate (goods): print(i,m) select =input("請選擇您要購買的商品:") if select.isdigit(): if 0<=int(select)<len(goods): s=goods[int(select)] a =s.get("price") print(a) if a >=money: print("賬戶余額不足")else: w = s.get("price") moneys =money-w print("余額還剩",moneys) lst.append(s) print(lst ) print("購買成功") else: print(‘選擇有誤,請重新選擇‘) elif select == ‘q‘: print(‘--------------選中商品列表-------------‘) for i,v in enumerate(lst): print(i,v) break
python 實現購物車的優化