python实现简单的循环购物车小功能

python实现简单的循环购物车小功能

python实现简单的循环购物车小功能

# -*- coding: utf-8 -*-
__author__ = 'hujianli' shopping = [
("iphone6s", 5000),
("book python", 81),
("iwach", 3200),
("电视机", 2200)
] def zero(name):
if len(name) == 0:
print("\033[31;1m您的输入不能为空,清重新输入~~\033[0m")
exit()
else:
return True def crre(name):
if int(name) not in range(len(shopping)):
print("\033[31;1m您输入了错误的序列号,请重新输入~~\033[0m")
exit()
else:
return True gongzi = input("请输入您目前的工资:")
if not gongzi.isdigit():
crre()
elif len(gongzi) == 0:
zero()
else:
gongzi = int(gongzi)
print("=========================================")
print("===要购买的列表清单如下: ")
for i,j in enumerate(shopping):
print(i, j[0], j[1]) shopping_cart = []
while True:
choice = input("请输入你要购买的商品,输入q或者exit退出选择回到购物车列表: ")
if choice.isdigit():
zero(choice)
crre(choice)
choice = int(choice)
goods = shopping[choice]
if gongzi < int(goods[1]):
gongzi -= goods[1]
print("===您的工资余额不够使用了,还差\033[1;31m{}\033[0m请努力挣钱===.".format(abs(gongzi)))
else:
print("您选购的商品:\033[1;32m %s \033[0m 已经加入到购物车.." % (goods[0]))
shopping_cart.append(goods)
gongzi -= goods[1]
print("您的工资余额还剩下:\033[30;42m %s\033[0m"% (gongzi))
elif (choice == "q") or (choice == "exit"):
break print('\033[30;42m你已购商品列表\033[0m'.center(42,'-'))
for i,j in enumerate(shopping_cart):
print("\033[1;32m" + str(i+1) + " " + str(j[0]) + " " + str(j[1]) + "\033[0m")
05-06 19:21