python列表购物

一,使用字典实现:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/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  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

二,使用列表实现:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/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  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  in  buy_list:
             print i
         break

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/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  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  in  buy_list:
                         print i
                 break










本文转自 deng304749970 51CTO博客,原文链接:http://blog.51cto.com/damondeng/1355196,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值