欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python 简单购物车

程序员文章站 2022-03-20 21:28:52
product_list =[ ('huawei',3000), ('hongmiNote3',3000), ('sanxing',2600), ('ThinkPad870',15000), ('Iphone8',5600), ('HuaweiP20',3300)]#商品列表shopping_lis ......
product_list =[
('huawei',3000),
('hongmiNote3',3000),
('sanxing',2600),
('ThinkPad870',15000),
('Iphone8',5600),
('HuaweiP20',3300)
]#商品列表
shopping_list = []#购物清单
while True:
salary = input('Input you salary:') #输入薪水
if salary.isdigit(): #判断输入的薪水是否是数字
salary = int(salary)
while True:
for index,item in enumerate(product_list): #打印商品列表
print(index,item)
user_choice = input('你要买什么?(退出请输入quit):') #输入商品序列号
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice > 0: #判断商品序列号是否在商品列表中
p_item = product_list[user_choice]
if p_item[1] <= salary: #判断是否买得起
shopping_list.append(p_item)
salary -= p_item[1]
print('Added %s into shopping car,your current balance is \033[31;1m%s\033[0m' %(p_item,salary))
else:
print("\033[41;1m你的余额只剩[%s]啦\033[0m" % salary)
else:
print('product_code [%s] is not excit'% user_choice)
elif user_choice == 'quit':
print('------shopping list-------')
for i in shopping_list:
print(i)
print('your current balance:%s' % salary)
exit()
else:
print('input true product_code,product_code is a number')
# break
else:
print('请正确输入你的薪水')