首页 > 代码库 > python 用列表实现简单购物功能

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 用列表实现简单购物功能