码迷,mamicode.com
首页 > 其他好文 > 详细

简单购物车

时间:2016-11-24 14:37:14      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:blog   main   商品   style   global   one   技术   列表   isp   

要求:

1、启动程序后,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够提示余额不足。

4、可随时退出,退出时,打印已购买商品和余额。

技术分享
#_*_ coding:utf-8 _*_
print("这是一个简单的购物程序!")
Salary=input("请输入您的余额:")
Product_list=[[1,Iphone7,5288],[2,Thinkpad x240,9000],[3,Mac book,12000],[4, Canon camera,6888]]
Buy_list=[]
Blance=0
def list_product():
    print("我们的商品如下:")
    i=0
    while i < len(Product_list):
        j=0
        while j < len(Product_list[i]):
            print(Product_list[i][j],end= )
            j=j+1
        print()
        i=i+1

def buy_main():
    Product_num = input("请输入您要购买的商品的编号:")
    global Blance
    if Product_num == q or Product_num == Q:
        print("我的购物车:", Buy_list[:])
        print("您的余额为:", Blance)
        exit()
    else:
        i=0
        while i < len(Product_list):
            j=0
            while j < len(Product_list[i]):
                if Product_list[i][j] == Product_num and Product_list[i][2] <= int(Salary):
                    print("您购买的商品为",Product_list[i][1],"价值",Product_list[i][2])
                    print("您的余额为:",int(Salary)-Product_list[i][2])
                    Blance=int(Salary)-Product_list[i][2]
                    Buy_list.append(Product_list[i][1:])
                    print("我的购物车:",Buy_list[:])
                elif Product_list[i][j] == Product_num and Product_list[i][2] > int( Blance):
                    print("对不起,您的余额不足!")
                    exit()
                else:

                    break
                j=j+1
            print()
            i=i+1
i = 0
while i <= 0:
    list_product()
    buy_main()
    i = i-1
View Code

 

简单购物车

标签:blog   main   商品   style   global   one   技术   列表   isp   

原文地址:http://www.cnblogs.com/pythonstudy/p/6097334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!