一,使用字典实现:

流程:输入工资->列出物品和价格->选择购买的物品->剩余工资->继续购物

#!/usr/bin/env python
salary=input('please input your salary:')
remain=salary
buylist=[]
shoplist={"coffee":35,
        "coat":200,
        "iphone":3000,
        "notebook":5000 }
while True:
        for i in shoplist:
                print '%s:' % i,shoplist[i]
        #print shoplist
        select=raw_input("please input shop name:")
        remain=remain - shoplist[select]
        if remain >= 0:
                buylist.append(select)
                print "your  remain monkey is %s,please to choose" % remain
        else:
                every = salary/22
                less = -remain/every
                print "you need back to work %s  day" % less
                print "you buy to shop is:" buylist

二,使用列表实现:


#!/usr/bin/env python
product = ['coat','coffee','bag','iphone','book','notebook']
price = [300,35,150,4000,100,6000]
buy_list=[]
salary = input('please input you salary:')
while True:
    print"welcome to market"
    for p in product:
        price_index=product.index(p)
        print "%s\t\t%s" %(p,price[price_index])
    if salary >= min(price):
        shop = raw_input('please  input shop name:')
        if shop in product:
            price_index=product.index(shop)
            print "%s\t%s" %(shop,price[price_index])
            if salary >= price[price_index]:
                buy_list.append(shop)
                salary = salary - price[price_index]
            else:
                print "Will not be able to buy,choose other."
                                         
        else:
            print "There is no such shop"
    else:
        print "Don't have enough money"
        print "have to buy:"
        for i in buy_list:
            print i
        break

第三种方式:将物品与价格写在文件中:

#!/usr/bin/env python
product=[]
price=[]
buy_list=[]
salary = input('please input you salary:')
f = file('shop.txt','r')
while True:
        print"welcome to market"
        for line in f.readlines():
                L = line.split()
                print L
                product.append(L[0])
                price.append(L[1])
                print product
                print price
        for p in product:
                price_index=product.index(p)
                print "%s\t\t%s" %(p,price[price_index])
        if salary >= min(price):
                shop = raw_input('please  input shop name:')
                if shop in product:
                        price_index=product.index(shop)
                        print "%s\t%s" %(shop,price[price_index])
                        if salary >= price[price_index]:
                                buy_list.append(shop)
                                salary = salary - price[price_index]
                        else:
                                print "Will not be able to buy,choose other."
                                  
                else:
                        print "There is no such shop"
        else:
                print "Don't have enough money"
                print "have to buy:"
                for i in buy_list:
                        print i
                break