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,如需转载请自行联系原作者
目录
相关文章
|
2天前
|
BI Python
深入浅出:讲解Python中的列表推导式
深入浅出:讲解Python中的列表推导式
|
2天前
|
监控 PHP Python
1688快速获取整店铺列表 采集接口php Python
在电子商务的浪潮中,1688平台作为中国领先的批发交易平台,为广大商家提供了一个展示和销售商品的广阔舞台;然而,要在众多店铺中脱颖而出,快速获取商品列表并进行有效营销是关键。
|
3天前
|
算法 Python
Python中不使用sort对列表排序的技术
Python中不使用sort对列表排序的技术
16 1
|
3天前
|
Python
【Python 基础】列表(list)和元组(tuple)有什么区别?
【5月更文挑战第6天】【Python 基础】列表(list)和元组(tuple)有什么区别?
|
3天前
|
算法 Python
从原始边列表到邻接矩阵:使用Python构建图的表示
从原始边列表到邻接矩阵:使用Python构建图的表示
7 0
|
3天前
|
机器学习/深度学习 存储 数据挖掘
Python中遍历并修改列表的综合指南
Python中遍历并修改列表的综合指南
13 2
|
3天前
|
机器学习/深度学习 自然语言处理 Python
python分词列表转化成词向量
python分词列表转化成词向量
9 1
|
3天前
|
算法 数据处理 Python
Python技术分享:如何将数据列表中的空值补0
Python技术分享:如何将数据列表中的空值补0
13 1
|
3天前
|
数据处理 Python
Python中按指定数量分割列表字符串的方法
Python中按指定数量分割列表字符串的方法
9 1
|
9天前
|
数据挖掘 数据处理 Python
【亮剑】如何在 Python 中以表格格式打印列表?
【4月更文挑战第30天】本文介绍了Python中以表格格式打印列表的三种方法:1) 使用字符串格式化,适用于简单场景;2) 使用prettytable库,适合需要更多格式化选项的情况;3) 使用pandas库,适用于处理大量数据和复杂分析。根据需求选择合适的方法来展示数据。