Python练习程序(二)文件处理和列表元组
来源:互联网 发布:js下拉框搜索功能 编辑:程序博客网 时间:2024/06/05 22:36
程序:
- 让用户输入薪资
- 输出商品及商品价格
- 计算用户能否支付
- 输出剩余的钱,问用户是否继续购物,直到钱不够为止
程序使用的shop.txt文件内容:
[root@localhost py]# cat shop.txt mac 10000iphone 5000tea 400coffee 35boots 999
===================================
#!/usr/bin/env python#coding=utf-8import sysf=file('shop.txt')products = []prices = []shop_list = []shop_count = [] #存储购买物品,计算同一物品购买数量for line in f.readlines(): new_line = line.split() #通过指定分隔符对字符串进行切片 products.append(new_line[0]) prices.append(int(new_line[1])) #默认str类型salary = int(raw_input('请输入你的工资:'))while True: print "~~~~~~~~~~~~~~~~~商品列表:~~~~~~~~~~~~~~~~~" for p in products: print p,'\t',prices[products.index(p)] choice = raw_input('请选择你购买的商品:') f_choice = choice.strip() #移除字符串头尾指定的字符(默认为空格) if f_choice in products: price = prices[products.index(f_choice)] if salary >= price: shop_list.append(f_choice) print f_choice,price,"已经添加到购物车中。" salary = salary - price print "您的余额:" ,salary if f_choice not in shop_count: shop_count.append(f_choice) else: if salary < min(prices): print"余额不足。一共买了这些商品:" for p in shop_count: print "%s\t*%d"%(p,shop_list.count(p)) sys.exit() #程序退出 else:print "对不起,您的余额:%d。请购买其他商品" %salary else:print"选择不在商品列表中。"
0 0
- Python练习程序(二)文件处理和列表元组
- python笔记二-列表和元组
- Python基础(二):列表和元组
- python学习笔记(二)列表和元组
- Python 3语法小记(二)列表 和 元组
- Python基础编程(二)列表和元组
- 《Python基础教程》(二)列表和元组
- Python序列:字符串、列表和元组(二)
- python学习笔记(二)列表和元组
- python(二)列表,元组,循环
- Python (二)列表、元组
- Python练习代码 -- 元组,列表,字典
- Python--列表和元组(1)
- Python 学习笔记(二) 列表和元组
- python列表和元组
- Python列表和元组
- Python 列表和元组
- Python 列表和元组
- 52种8阶环(2015-11-18 17:21:51)
- android RTL布局支持说明
- 分析函数进阶(一)
- Dubbo:关于与高版本的Spring集成的事情
- iOS tableView的cell的重用问题
- Python练习程序(二)文件处理和列表元组
- hdu 3460
- IOS通知机制
- 堆栈形象说明
- UILabel的换行问题
- WCF列子
- 销售发货自动批次
- 【Solutions系列】目标危险性分析
- android6.0源码编译及刷机