商品清单,购物清单
procuct_name1=[["苹果7 Plus",5700],
["小米5 Plus",2600],
["华为P10",4088],
["锤子M1",2499],
["魅族6 Plus",2999]]
shopping_cart = [] #购物车
shopping_name=[] #选购的商品名
expense = 0 #总消费
#首页
print("欢迎来到手机大卖场!!!!!")
salary = int(input("请输入您携带的现金:").strip())
print("下面是本店的商品列表:
---------------------------")
#开始购物。
while True :
# 将商品清单取出和价钱匹配排列。
print("{0:4}{1:9}{2:9}".format("编号","商品名","单价"))
for i in range(0, len(procuct_name1)):
print(("{0:^4}{1:9}{2:6}" .format((i+1),procuct_name1[i][0], procuct_name1[i][1])))
else:
print("---------------------------")
choice=input("请输入您想购买的商品编号:(如果想退出商店请输入:q)").strip()
if choice.isdigit():
choice=int(choice)
elif choice=="q":
break
else:
print("没有此选项!")
continue
if choice > len(procuct_name1) or choice <0:
print("请输入正确的商品编号!")
else:
money = procuct_name1[choice-1][1]
if money > salary:
diff = money - salary
print("您不能购买本商品,您的余额为:%d 还差 %d" % (salary,diff))
else:
if procuct_name1[choice-1][0] in shopping_name:
i = shopping_name.index(procuct_name1[choice-1][0])
shopping_cart[i][2] = shopping_cart[i][2] + 1
else:
shopping_cart.append(procuct_name1[choice - 1])
shopping_cart[len(shopping_cart)-1].append(1)
shopping_name.append(procuct_name1[choice-1][0])
salary = salary - money
expense += money
print("购物成功! 余额为: %d" % salary)
#结账。打印购物清单。
if len(shopping_cart) == 0:
print("欢迎下次光临!")
else:
print("---------------------------")
print("您本次购买的商品:")
print("{0:4}{1:9}{2:9}{3:9}{4:9}".format("编号", "商品名", "单价","数量","总价"))
for i in range(0,len(shopping_cart)):
total = shopping_cart[i][1]*shopping_cart[i][2]
print(("{0:^4}{1:9}{2:6}{3:9} {4:9}".format((i + 1),
shopping_cart[i][0],
shopping_cart[i][1],
int(shopping_cart[i][2]),total)))
else:
print("总消费:%d 余额为:%d" % (expense,salary))
print("欢迎下次光临!
--------------------------------")