Python购物车的实现课程

Python购物车的实现课程

需求:

1.用户输入工资收入

2.打印商品列表

3.用户选择商品,不断的加入购物车

4.检测用户余额,直接捐款,不足提示余额不足

5.允许主动退出,退出时,打印已购商品列表

重点方法:

打印列表下标的方法:

a=['alex','sys','root','admin']

>>> for index,i in enumerate(a):
...     print(index,i)

0 alex
1 sys
2 root
3 admin

 

 1 #!/usr/bin/env python3
 2 # -*-conding:utf-8-**
 3 # __Author__:'liudong'
 4 salary=input("Input your salary:")
 5 if salary.isdigit():
 6     salary=int(salary)
 7 else:
 8     exit("Invalid data type...")
 9 welcom_msg='Welcome to Shopping mall'.center(50,'-')
10 print(welcom_msg)
11 exit_flag=False
12 product_list = [
13     ('Iphone',5000),
14     ('Mac Air',8000),
15     ('Mac Pro',9000),
16     ('XiaoMi',20),
17     ('Coffe',30),
18     ('Bike',800),
19     ('Cloth',200)
20 ]
21 shop_car = []
22 while not exit_flag:
23     #for product_item in product_list:
24     #    p_name,p_price = product_item  #可选的写法
25     print("Products list".center(50,'-'))
26     # for p_name,p_price in product_list:
27         # print(p_name,p_price)         #由于此方法后面打印下标时,会变成2个无组,所以用下面的方法打印
28     for item in enumerate(product_list):
29         index=item[0]
30         p_name=item[1][0]
31         p_price=item[1][1]
32         print(index,'.',p_name,p_price)
33     user_choice = input('[q=quit,c=check]What do you want to buy?:')
34     if user_choice.isdigit():   #肯定是选商品
35         user_choice = int(user_choice)
36         if user_choice < len(product_list):
37             p_item = product_list[user_choice]
38             if p_item[1] <= salary: #买的起
39                 shop_car.append(p_item) #放入购物车
40                 salary -= p_item[1] #扣钱
41                 print('Added [%s] into your shop car,your current balance is \033[31;1m[%s]\033[0m' %
42                   (p_item,salary))   #字体加颜色
43             else:
44                 print('Your balance is [%s],cannot afford this product..' %salary)
45     else:
46         if user_choice == 'q' or user_choice == 'quit':
47             print('purchased products as blew:'.center(40,'*'))
48             for item in shop_car:
49                 print(item)
50             print('END'.center(40,'*'))
51             print('Your balance is [%s]' %salary)
52             print('bye.')
53             exit_flag = True
54         elif user_choice == 'c' or user_choice == 'check':
55             print('purchased products as blew:'.center(40, '*'))
56             for item in shop_car:
57                 print(item)
58             print('Your balance is \033[41;1m[%s]\033[0m' % salary) #背景加颜色
View Code

 

 

 

编程的思维还要加强。。。。。

 

posted @ 2016-10-22 12:22 ld1977 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值