python 用列表实现简单购物功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用列表实现简单购物功能相关的知识,希望对你有一定的参考价值。

import sys

products = [‘PC‘,‘Coffee‘,‘TV‘,‘Car‘]

shoplist = []

prices = [5000,50,800,20000]

salary = input(‘please input your salary:‘)

while True: 

    for i in products:

        print ‘i‘,‘prices[products.index(i)]‘

        choice = input‘please choose your need:‘

        choice2 = choice.strip()

        if choice2 in products:

            product_price_index = products.index(choice2)

            product_price = prices[product_price_index]

            if salary > product_price:

                shoplist.append(choice)

                print‘%s has joined your shoppinglist‘ % choice2

                salary = salary - product_price

                print‘your salary left:‘,salary

            else:

                if salary < min(prices)

                    print‘Sorry,your salary cannot buy anything! ‘

                    print‘this is your shoppinglist:‘,shoplist

                    sys.exit()

                else:

                    print‘Sorry ,youcannot afford this product‘

  

以上是关于python 用列表实现简单购物功能的主要内容,如果未能解决你的问题,请参考以下文章

python实际练习1——简单购物车

python学习实例:写一个简单商城购物车的代码

Python学习——出拳游戏+购物(列表)

python实现简单的循环购物车小功能

python实现简单的购物程序

原生JavsScript实现简单购物车面向对象原生代码