python编写一个超市购物小系统案例

# author:Iron Qi

product_list = [('Iphone',5800),

                ('Mac Pro',9800),

                ('Play Game',1000),

                ('Watch',4300),

                ('Basketball',260),

                ('cloth',1314)

              ]

shopping_list=[]  #商品购物车是空表

salary = input("Input your salary:")

if salary.isdigit():

    salary = int(salary)

    while True:

        for index,item in enumerate(product_list): # 取列表的下标和实际属性

            print(index,item)

        user_choice = input("Please select the item you want to buy:") # 选择的是商品编号

        if user_choice.isdigit():

            user_choice = int(user_choice)

            if user_choice < len(product_list)and user_choice >=0:

                p_price=product_list[user_choice]

                if p_price[1] <= salary: #can buy

                    shopping_list.append(p_price)

                    salary -=p_price[1]

                    print("Added %s into shopping cart,your balance is \033[31;1m%s\033[0m" %(p_price,salary))

                else:

                    print("\033[32;1myour balance only is[%s],No longer available\033[0m"% salary)

            else:

                print("product code [%s] is not exist!"% user_choice)

        elif user_choice =='q':

            print("---------shopping list---------")

            for p in shopping_list:

                print(p)

            print("your current balance:",salary)

            exit()

        else:

            print("invalid option")

else:

    print("\033[33;1mYour input is not valid\033[0m") #\033[33;1m----\033[0m 是用来高亮。


结果展示:

Input your salary:5000

0 ('Iphone', 5800)

1 ('Mac Pro', 9800)

2 ('Play Game', 1000)

3 ('Watch', 4300)

4 ('Basketball', 260)

5 ('cloth', 1314)

Please select the item you want to buy:4

Added ('Basketball', 260) into shopping cart,your balance is 4740

0 ('Iphone', 5800)

1 ('Mac Pro', 9800)

2 ('Play Game', 1000)

3 ('Watch', 4300)

4 ('Basketball', 260)

5 ('cloth', 1314)

Please select the item you want to buy:5

Added ('cloth', 1314) into shopping cart,your balance is 3426

0 ('Iphone', 5800)

1 ('Mac Pro', 9800)

2 ('Play Game', 1000)

3 ('Watch', 4300)

4 ('Basketball', 260)

5 ('cloth', 1314)

Please select the item you want to buy:q

---------shopping list---------

('Basketball', 260)

('cloth', 1314)

your current balance: 3426

0 ('Iphone', 5800)

1 ('Mac Pro', 9800)

2 ('Play Game', 1000)

3 ('Watch', 4300)

4 ('Basketball', 260)

5 ('cloth', 1314)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • product_list = [ ('Iphone',7000), ('Mac Pro',12000), ('By...
    Kira_8768阅读 334评论 0 0
  • 购物车程序需求: 1、启动程序后,让用户输入工资,然后打印商品列表 2、允许用户根据商品编号购买商品 3、用户选择...
    凉快2019阅读 160评论 0 0
  • shopping_list = []product_list = [('iphone',5800),('mac p...
    南少cc阅读 1,234评论 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,424评论 0 10
  • 我的图书馆 留言交流 2000个最常用的英语单词 2016-05-15酴羰骀璺 2000个最常用的英语单词 (英语...
    Mr_Wang92阅读 709评论 0 0