练习-购物车
需求
- 用户启动的时先输入工资
- 用户启动程序后打印商品列表
- 允许用户选择购买商品
- 允许用户不断的购买各种商品
- 购买时检余额是否足够,如果足够直接扣款,否则打印余额不足
- 允许用户主动退出程序,退出时打印已购列表
salary = input('please input salary :')
if salary.isdigit() :
salary = int(salary)
else:
print('Invaild data type ...')
exit()
welcome_msg = 'welcome to Shooping mall'.center(50,'-')
print(welcome_msg)
product_list = [
('iphone',5888),
('Mac Air',8888),
('Mac Pro',9999),
('XiaoMi 2 ',19),
('Coffee',30),
('Tesla',820000),
('Bike',900),
('Cloth',200)
]
shop_car = []
exit_flag = False
while not exit_flag:
print('product list'.center(50,'-'))
for i,item in enumerate(product_list):
p_name,p_price = item
print('%s. %s %s元' %(i,p_name,p_price))
user_choice = input('[q=quit,c=check]What do you want to buy?:')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list):
p_item = product_list[user_choice]
if p_item[1] <= salary :
shop_car.append(p_item)
salary -= p_item[1]
print('Added \033[32;1m[%s]\033[0m into shop car,you current balance is [%s]'
%(p_item,salary))
else:
print('Your balance is [%s],cannot afford this ..' % salary)
else:
print('Please Choose the right goods ... ')
elif user_choice == 'c' or user_choice == 'check':
print('purchased product'.center(50,'*'))
for item in shop_car:
print(item[0],item[1])
print('END'.center(50,'*'))
print('Your balance is [%s]' % salary)
elif user_choice == 'q' or user_choice =='quit' :
print('purchased product'.center(50,'*'))
for item in shop_car:
print(item[0],item[1])
print('END'.center(50,'*'))
print('Your balance is \033[31;1m[%s]\033[0m' % salary)
exit_flag = True
else:
print('Please Choose the right option ... ')