码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-03 20:10:01      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:this   sys   logs   pytho   str   highlight   实现   list   index   

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

标签:this   sys   logs   pytho   str   highlight   实现   list   index   

原文地址:http://www.cnblogs.com/xiaoxinzang/p/7281276.html

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