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,如需转载请自行联系原作者
目录
相关文章
|
1天前
|
Python
Python 选出列表中特定的元素
Python 选出列表中特定的元素
10 3
|
1天前
|
数据处理 索引 Python
Python列表与元素修改的操作技巧
Python列表提供了丰富的方法和技巧来进行高效的数据操作。熟练运用上述技巧,可以大大提高数据处理的效率和代码的可读性。实践中,根据具体需求灵活选择合适的方法,可以在保证代码效率的同时,也使代码更加简洁明了。
12 2
|
2天前
|
Python
python之列表生成式
python之列表生成式
|
1天前
|
Python
探索Python中的列表推导式
在本文中,我们将深入探讨Python中一个强大且灵活的特性——列表推导式。列表推导式是一种简洁而优雅的方法,用于创建和操作列表。它不仅使代码更易读,还能提高开发效率。通过几个示例,我们将展示列表推导式的多种应用,从基本的操作到复杂的组合,帮助读者更好地理解和利用这一工具。
7 1
|
4天前
|
程序员 数据处理 开发者
探索Python中的列表推导式
【9月更文挑战第23天】在Python编程的海洋中,列表推导式如同一艘快速且灵活的小艇,让开发者能够以简洁的代码处理复杂的数据操作。本文将深入分析列表推导式的工作机制,展示其在不同场景下的强大功能,并探讨如何高效运用这一工具来提升代码的可读性和性能。
|
9天前
|
存储 数据处理 索引
Python列表操作的方法总结
通过掌握上述方法,你可以有效地操作Python列表,完成各种数据处理任务。列表的灵活性和多功能性使其成为Python编程中不可或缺的工具。
19 1
|
13天前
|
数据采集 算法 数据处理
Python中的列表推导式:简介与应用
【9月更文挑战第14天】本文旨在介绍Python中一种强大且简洁的构造列表的方法——列表推导式。我们将从基础语法入手,通过实例演示其用法,并探讨在数据处理和算法优化中的应用价值。文章将不包含代码示例,而是专注于概念理解和应用场景的描述,以促进读者对列表推导式的深入认识。
18 3
|
14天前
|
数据采集 数据处理 C语言
探索Python中的列表推导式
本文将深入探讨Python中强大的列表推导式功能,通过实例展示其基本语法、高级用法以及性能优势。我们将从简单的数值操作入手,逐步过渡到处理复杂数据结构,如嵌套列表和字典。此外,文章还将讨论列表推导式在提高代码可读性和减少运行时错误方面的实际价值,并通过与循环语句的对比,揭示其在特定场景下的性能优势。
|
15天前
|
数据处理 开发者 Python
探索Python中的列表推导式
在Python编程中,列表推导式(List Comprehensions)是一种简洁而强大的工具,允许开发者用一行代码生成整个列表。本文将深入探讨列表推导式的用法、优势以及在实际项目中的应用。通过具体的示例,我们将展示如何利用列表推导式简化代码,提升可读性和执行效率。无论你是编程新手还是经验丰富的开发者,都能从本文中获得有价值的见解和技巧。
|
19天前
|
C语言 Python
深入理解并实践Python中的列表推导式
深入理解并实践Python中的列表推导式
11 1