标签:python
员工信息表
用户可以模糊查询员工信息
显示匹配了多少条,匹配字符需要高亮显示
#!/usr/bin/env python #coding:utf-8 #author:yangyue import sys count = 0 key = raw_input(u‘请输入要查询的关键字:‘) if key == ‘q‘: sys.exit print ‘已退出程序‘ else : f = file(‘file.txt‘) for line in f.readlines(): if line.find(key) != -1: print line.replace(key,‘\033[31;1m %s \033[0m‘ %key) count += line.count(key) print ‘\n‘ print u‘共匹配到‘,count,‘次‘
2. 购物车程序
要求用户输入工资,然后打印购物菜单
用户可以不断的购买商品,直到钱不够为止
退出时格式化打印用户已购买的商品和剩余金额
#!/usr/bin/env python
#coding:utf-8
#author:yangyue
import os
shopping_list = {‘iphone‘:6000,‘ipad‘:2000,‘ipod‘:1000}
print ‘欢迎来到购物商城!‘
salary = int(raw_input(u‘请输入您的工资单:‘))
while salary >= 1000:
for i in shopping_list:
print i, shopping_list[i]
shop = raw_input(u‘\n请选择你要购买的商品:‘)
a = shopping_list.has_key(shop)
f = file(‘shopping‘,‘w‘)
f = file(‘shopping‘,‘a‘)
f.write(shop)
f.write(‘\n‘)
if a == True:
salary -= shopping_list[shop]
if salary >= 0:
print u‘你现在的余额为‘,‘\033[31;1m %s \033[0m‘ %salary,u‘元‘
# f.write(‘%s,%s\n‘ %(shop,shopping_list[shop]))
# f.write(‘\n‘)
con = str(raw_input(u‘还想继续购物吗?(y/n)‘))
if con == ‘n‘:
#f.write(str(salary))
f.close()
print u‘您已退出购物商城‘
print u‘您所购买的商品为:‘
f = file(‘shopping‘,‘r‘)
for line in f.readlines():
print line,
print u‘您的余额为:\033[31;1m %s \033[0m‘ %salary
break
else:
print ‘没有此商品‘
else:
print ‘\n您的余额不足,不能继续购物!‘本文出自 “运维工作笔记” 博客,请务必保留此出处http://yyyummy.blog.51cto.com/8842100/1616926
标签:python
原文地址:http://yyyummy.blog.51cto.com/8842100/1616926