1.检验注册用户是否合法;需要输入用户名,校验用户名是否被注册,如已注册,提示已经注册过,没注册就可以注册;用户名不能为空;用户名长度必须在6-13位之间;最多只能输入三次。

  users = ['aatest1','aatest2','aatest3']
for i in range():
username = input('请输入用户名:').strip()
if len(username)> and len(username)<:
if username in users: #if users.count(username)>0
print('这个名字太好了,已经被注册!')
else:
print('这个名字不错,赶紧注册吧!')
break
else:
print('用户名应该是6-12位!')
else:
print('失败次数太多!')

2.输出已完成工作的员工和未完成工作的员工,并分别统计人数

stus=[
['小明','未完成'],
['小白','已完成'],
['小紫','已完成'],
['小红','未完成'],
['小绿','未完成'],
['小黄','未完成'],
['小黑','已完成']
]
  stus=[
['小明','未交'],
['小白','已交'],
['小紫','已交'],
['小红','未交'],
['小绿','未交'],
['小黄','未交'],
['小黑','已交']
]
pass_list = []
fail_list = []
for stu in stus:
stuname=stu[]
status=stu[]
if status=='未交':
fail_list.append(stuname)
else:
pass_list.append(stuname)
print('未交作业的同学是:%s,总共有%s人'%(fail_list,len(fail_list)))
print('已交作业的同学是:%s,总共有%s人'%(pass_list,len(pass_list)))

3.检验注册用户是否合法;需要输入用户名,校验用户名是否被注册,如已注册,提示已经注册过,没注册就可以注册;用户名不能为空;用户名长度必须在6-13位之间;校验密码和确认密码是否一致;最多只能输入三次;输出所有注册成功的账号。

  users={'cctest1':'','cctest2':''}
for i in range():
uname=input('uname:').strip()
pwd=input('pwd:').strip()
cpwd=input('cpwd:').strip()
if uname=='' or pwd=='' or cpwd=='':
print('用户名/密码不能为空')
elif pwd!=cpwd:
print('密码和确认密码不一致')
elif uname in users:
print('用户名已注册')
else:
print('恭喜,注册成功')
users.setdefault(uname,pwd)
break
else:
print('失败次数太多')
print(users)

4.校验密码是否合法的程序。
输入一个密码
    1、长度5-10位
   2、密码里面必须包含,大写字母、小写字母和数字
   3、最多输入5次

 for i in range():
list=[]
passwd=input("请输入密码:").strip()
if len(passwd)> and len(passwd)<:
for i in passwd:
if i.isdigit()==True:
list.append()
elif i.isupper()==True:
list.append()
elif i.islower()==True:
list.append()
else:
print('输入不合法')
break
if in list and in list and in list:
print("密码合法,请注册!")
else:
print("密码必须由大写字母、小写字母和数字组成!")
else:
print('用户名长度不合法!长度5-10之间!')
else:
print('失败次数过多!')

5.写一个录入学生作业情况的一个程序
     1、查看学生作业情况
     2、录入学生作业情况
     3、可以让输入3次,需要为空的情况
     homeworks = {
                '张流量':
                            {'2018.3.22':"未交",'2018.3.23':'wei交'},
                '田雨':

{'2018.3.22':"未交",'2018.3.23':'wei交'},
}

 homeworks  ={
'张流量': {
'2018-03-31':"未交"
},
'李wifi':{
'2018-03-31':"未交"
}
}
choice = input('请输入你的选择,1查看作业,2录入:')
if choice=='':
for k,v in homeworks.items():
print('【%s】的作业情况是 %s'%(k,v))
elif choice=='':
for i in range():
name = input('输入名字:').strip()
time = input('输入日期:').strip()
status = input('输入状态:').strip()
if name =='' or time =='' or status=='':
print('输入不能为空')
else:
if name in homeworks:
tmp = {time:status}
homeworks[name].update(tmp)
print('更新学生作业信息成功')
break
else:
homeworks[name]={time:status}
print('成功')
break
print(homeworks)

6.你是一个高级测试工程师,现在要做性能测试,需要你写一个函数,批量生成一些注册使用的账号。

产生的账号是以@163.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大写字母、小写字母、数字组成,结果如下图:

import random,string
acc_set = set()
length = input('请输入需生成的账号长度:')
count = input('请输入要生成的账号个数:')
if count.isdigit():
count = int(count)
while len(acc_set) != count:
upper = random.choice(string.ascii_uppercase)
lower = random.choice(string.ascii_lowercase)
number = str(random.randint(,))
other_length = int(length) -
other_string = random.sample(string.digits+string.ascii_letters,other_length)
account_list = [upper , lower , number] + other_string
random.shuffle(account_list)
print(account_list)
account = ''.join(account_list)+'@163.com'
acc_set.add(account+'\n')
else:
open('account.txt','w').writelines(acc_set)
else:
print('输入的账号个数必须是整数!')

7.随机生成指定数量的电话号码,号码以1861235开头。

 import random
start = ''
f = open('phone.txt','w+',encoding='utf-8')
num = input('请输入你想生成的电话号码个数:')
for i in range(int(num)):
random_num = str(random.randint(,))
new_num = random_num.zfill()
phone = start+new_num
f.write(phone+'\n')
f.close()

8.公司服务器,经常被别人攻击,要写个监控nginx日志的脚本,每分钟运行一次,如果这一分钟内同一个ip请求次数超过200次,加入黑名单,nginx日志每一行的格式如下:

 46.161.9.44 - - [/Jun/::: +] "GET /bbs/forum.php?mod=forumdisplay&fid=2 HTTP/1.0"   "http://aaaa.bbbbb.com/bbs/forum.php?mod=forumdisplay&fid=2" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
46.161.9.44 - - [/Jun/::: +] "GET /bbs/forum.php?mod=forumdisplay&fid=2 HTTP/1.0" "http://aaaa.bbbbb.com/bbs/forum.php?mod=forumdisplay&fid=2" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
 import time

 point =   # 每次记录文件指针的位置
while True:
all_ips = [] #存放所有的ip地址
f = open('access.log',encoding='utf-8')
f.seek(point)#移动文件指针
for line in f:
ip = line.split()[]#先按照空格分割元素,然后取第一个元素,就是IP地址
all_ips.append(ip)
point = f.tell() #记录了指针的位置
all_ip_set = set(all_ips) #
for ip in all_ip_set:
if all_ips.count(ip)>:
print('应该加入黑名单的ip是%s'%ip)
f.close()
time.sleep() #暂停60秒

9.写一个自动生成密码文件的程序

    1、你输入几,文件里面就给你产生多少条密码
2、密码必须包括,大写字母、小写字母、数字、特殊字符
3、密码不能重复
4、密码都是随机产生的
5、密码长度6-11
http://www.nnzhp.cn/archives/150
随机数怎么用 参考 random模块怎么用
 import string,random
pwd_len = input('请输入你要产生多少条密码:').strip()
pwds = set() #存放所有的密码
if pwd_len.isdigit():
pwd_len = int(pwd_len)
while len(pwds)!=pwd_len:
num=random.choice(string.digits)
letter = random.choice(string.ascii_lowercase)
upper = random.choice(string.ascii_uppercase)
pun = random.choice(string.punctuation)
pasd_len = random.randint(,) #代表生成密码的长度
other_len = pasd_len - #剩余的长度
all_strs = string.digits+string.ascii_letters+string.punctuation
other_passwd = random.sample(all_strs,other_len)#随机取到剩下的密码
pwd_list = [num,letter,upper,pun]+other_passwd #产生密码之后的list
random.shuffle(pwd_list)#顺序打乱
pwd_str = ''.join(pwd_list) #最终的密码
pwds.add(pwd_str+'\n')
else:
open('passwds.txt','w').writelines(pwds) else:
print('条数必须是整数!')
10.写一个注册的程序,账号和密码都存在文件里面。
    choice = input('请输入你的选择:1,注册2、删除用户3、登录')
#注册
输入
账号
密码
密码确认
#需要校验用户是否存在,两次输入的密码,是否一致,为空的情况
#账号和密码都存在文件里面
#删除
输入一个用户名
#需要校验用户是否存在
#登录
输入账号密码登录
 user_info = {} #存放所有的用户
with open('users.txt') as f:
for line in f:
# niuhanyang,\n
line = line.strip()
temp = line.split(',')
username = temp[]
pwd = temp[]
user_info[username]=pwd
for i in range():
choice = input('请输入你的选择'
'1、登录 2、注册 3、删除').strip()
if choice=='':
username = input('username:').strip()
pwd = input('pwd:').strip()
if username and pwd:
if username in user_info:
if user_info.get(username)==pwd:
print('登录成功')
else:
print('账号密码错误!')
else:
print("user not found!")
else:
print('账号密码不能为空!')
elif choice=='':
username = input('username:').strip()
pwd = input('pwd:').strip()
cpwd = input('cpwd:').strip()
if username and pwd and cpwd:
if username in user_info:
print('该用户已经被注册!')
else:
if pwd==cpwd:
user_info[username]=pwd
print('恭喜,注册成功!')
else:
print('两次输入的密码不一致!')
else:
print('不能为空!')
elif choice=='':
username = input('username:').strip()
if username:
if username in user_info:
user_info.pop(username)
print('删除成功!')
else:
print('不能为空!')
else:
print("输入有误,请重新输入")
else:
with open('users.txt','w') as fw:
for uname,pwd in user_info.items():
fw.write(uname+','+pwd+'\n')
11、写一个生成双色球号码的一个程序,生成的号码写到文件里面
# 中奖号码由6个红色球号码和1个蓝色球号码组成
# 篮球范围:01-16
# 红球范围:01-33
def swq(num):
random.ranint(1,16)
#tikti.txt
篮球:xx 红球号码是xx 01 08 09 12 13 19
篮球:xx 红球号码是xx 01 08 09 12 13 19
 import random

 def seq():
red_num = [] #红球
while len(red_num)!=:
# -
num = random.randint(,)
num = str(num).zfill()
if num not in red_num:
red_num.append(num)
blue_num = str(random.randint(,)).zfill()
red_num_str = ' '.join(red_num)
res = '篮球是 %s 红球是 %s\n'%(blue_num,red_num_str)
return res def write_file(l):
with open('seq.txt','w',encoding='utf-8') as fw:
fw.writelines(l) def main():
all_res = [] #存所有结果的
num = input('请输入你要产生多少条:').strip()
if num.isdigit():
num = int(num)
while num != len(all_res):
res = seq()
if res not in all_res:
all_res.append(res)
else:
print('条数只能是整数!')
write_file(all_res)
main()
12、商品管理的程序,商品信息都存在一个json串里面
  校验商品是否存在  
1、查询商品信息
2、新增商品 ##校验价格是否合法 #校验商品是否存在
3、修改商品信息 ##校验商品是否存在
if chice =="1":
query_goods()
elif choice = ="2":
add_goods()
import json

FILE_NAME = 'goods.json'
def op_file(name,content=None):
if content:
with open(name,'w',encoding='utf-8') as fw:
json.dump(content,fw,indent=,ensure_ascii=False)
else:
with open(name,encoding='utf-8') as fr:
res = json.load(fr)
return res
all_goods = op_file(FILE_NAME) def check_price(price):
price = str(price)
if price.isdigit():
price = int(price)
if price>:
return True
else:
if price.count('.')==:
tmp = price.split('.')
#0.0
left = tmp[]
right = tmp[]
# 1.00 if left.isdigit() and right.isdigit() and int(right)>: #1.0
return True
elif left.isdigit() and right.isdigit() and int(left)>: # 0.1
return True
return False def get_good_info():
while True:
good_name = input('商品名称:').strip()
price = input('price:').strip()
count = input('count:').strip()
color = input('color:').strip()
if good_name and price and count and color:
if not check_price(price):
print('价格输入不合法,必须大于0')
elif not count.isdigit() and int(count)<:
print('数量不合法')
else:
return good_name,price,count,color
else:
print('输入不能为空!') def add_good():
good_name,price,count,color = get_good_info()
if good_name not in all_goods:
all_goods[good_name]= {
'price':price,
'count':count,
'color':color
}
op_file(FILE_NAME,all_goods)
print('添加完成!')
else:
print('商品已经存在!') def update_good():
good_name,price,count,color = get_good_info()
if good_name in all_goods:
all_goods[good_name]= {
'price':price,
'count':count,
'color':color
}
op_file(FILE_NAME,all_goods)
print('修改完成!')
else:
print('商品不存在!') def query_good():
good_name = input('商品名称:').strip()
if good_name in all_goods:
print(all_goods.get(good_name))
else:
print('商品不存在') def delete_good():
good_name = input('商品名称:').strip()
if good_name in all_goods:
all_goods.pop(good_name)
op_file(FILE_NAME,all_goods)
else:
print('商品不存在') def main():
for i in range():
choice = input('请输入你的选择'
'1、添加'
'2、修改'
'3、删除'
'4、查看'
'5、退出')
if choice=="":
add_good()
elif choice=="":
update_good()
elif choice=="":
delete_good()
elif choice=="":
query_good()
elif choice=="":
quit('程序退出')
else:
print('输入错误,请重新输入!')
return main()
main()
13、logs目录下,有一部分文件是空的
    1、删除log目录下,所有的空文件
2、删除5天前的文件
import os,datetime,time
for abs_path,dir,file in os.walk(r'D:\syz_python\code\day6\logs'):
for f in file:
file_name = os.path.join(abs_path, f) #拼接文件和绝对路径
size = os.path.getsize(file_name) #获取文件大小
file_date = f.split('.')[].split('_')[-] #获取文件日期
five_days = str(datetime.date.today() + datetime.timedelta(days=-)) #将五天前日期转化为str
if file_date<five_days or size ==:
os.remove(file_name)
14、写代码实现,把我的数据库里面的stu表中的数据,导出到excel中
    #编号 名字 性别
import pymysql,xlwt
coon = pymysql.connect(
host='118.24.3.40',user='jxz',passwd='',
port=,db='jxz',charset='utf8'
#port必须写int类型,
#charset这里必须写utf8
)
cur = coon.cursor() #建立游标
cur.execute('select * from stu;')#执行sql语句
res = cur.fetchall() #获取所有返回的结果
print(res)
title = ['编号','姓名','性别']
book = xlwt.Workbook() #新建一个excel
sheet = book.add_sheet('stu_info')
for i in range(len(title)):
sheet.write(,i,title[i])
row =
for i in res:
col =
for j in i:
sheet.write(row,col,j)
col+=
row+=
book.save('stu_info.xls')
cur.close()#关闭游标
coon.close()#关闭连接
15、注册  登录。数据都存在数据库里面
    id username passwd
注册的时候,密码存的是加密之后的密码
username pwd cpwd,都是必填的
用户不能重复
登录
账号
密码
登录成功之后打印当前的日期
def my_db(host,user,passwd,db,sql,rec,port=,charset='utf8'):
import pymysql
coon = pymysql.connect(user=user,
host=host,
port=port,
passwd=passwd,
db=db,
charset=charset
)
cur = coon.cursor() #建立游标
cur.execute(sql,rec)#执行sql
if sql.strip()[:].upper()=='SELECT':
res = cur.fetchall()
else:
coon.commit()
res = 'ok'
cur.close()
coon.close()
return res def my_md5(pwd):
import hashlib
new_pwd = pwd.encode()
m = hashlib.md5()
m.update(new_pwd)
return m.hexdigest()
import datetime
for i in range():
choice = input('请输入你的选择: 1.登录 2.注册')
if choice=='':
name = input('username:').strip()
pwd = input('pwd:').strip() if name and pwd :
s_pwd = my_md5(pwd)
res = [name, s_pwd]
result = my_db(host='192.168.21.129',user='root',passwd='',port=,db='test',charset='utf8',sql = 'select * from user where username=%s and passwd=%s',rec = [name,s_pwd])
if result!=():
print('登录成功!',datetime.datetime.today())
else:
print('用户名或密码错误!')
else:
print('用户名密码不能为空!')
elif choice == '':
name = input('username:').strip()
pwd = input('pwd:').strip()
cpwd = input('cpwd:').strip()
if name and pwd and cpwd:
sql = 'select username from user where username = %s'
sql_name = my_db(host='192.168.21.129',user='root',passwd='',port=,db='test',charset='utf8',sql = 'select username from user where username = %s',rec = [name])
if sql_name != ():
print('该用户已注册!')
else:
if pwd != cpwd:
print('两次输入的密码不一致!')
else:
s_pwd = my_md5(pwd)
my_db(host='192.168.21.129', user='root', passwd='', port=, db='test', charset='utf8',
sql="insert into user(username,passwd) values(%s,%s)", rec = [name, s_pwd])
print('恭喜你,注册成功!')
else:
print('输入不能为空!')

16、修改excel,把app_student.xls里面的数据,

1、如果这一行数据里面有乱码,那么就给他删掉

2、再加上一列,是否毕业

        3、如果班级是天蝎座的话,毕业这一列写成毕业

4、其他班级的写成未毕业

import xlrd
from xlutils import copy
book = xlrd.open_workbook('app_student.xls')
sheet = book.sheets()[]
new_book = copy.copy(book)# 通过xlutils这个模块里面copy方法,复制一份excel
sheet1 = new_book.get_sheet() # 获取sheet页
table = sheet.row_values()
table.append('是否毕业')
for col,field in enumerate(table):
sheet1.write(, col, field)
print(sheet.row_values())
tmp =
for row in range(,sheet.nrows):
list = sheet.row_values(row)
if '?' in str(list):
continue
else:
if list[table.index('grade')] == '天蝎座':
list.append('已毕业')
else:
list.append('未毕业')
for col,data in enumerate(list):
sheet1.write(tmp,col,data)
tmp+=
new_book.save('app_student.xls')
17、

最新文章

  1. ul li设置横排,并除去li前的圆点
  2. redis-删除所有key
  3. 跳过 centos部署 webpy的各种坑
  4. Hadoop:Windows 7 32 Bit 编译与运行
  5. 低功耗蓝牙4.0BLE编程-nrf51822开发(3)
  6. JavaScript基础知识之——Location 对象详解
  7. iOS9/iOS8界面对比 XCode7
  8. HDU 4576
  9. CSLight研究院之学习笔记结合NGUI(一)
  10. [HIHO1176]欧拉路&#183;一(欧拉图判定)
  11. NGINX开篇
  12. Application.HookMainWindow完全替代了原来的窗口过程(但是好像也会继续传递)
  13. [bzoj3998][TJOI2015]弦论-后缀自动机
  14. windows.go
  15. WinForm 水晶报表的简单使用
  16. mysql 查询优化~sql优化通用
  17. 实用的 图片上传 html+css
  18. [LOJ 6029]「雅礼集训 2017 Day1」市场
  19. python学习笔记013——推导式
  20. CSS学习笔记(6)--浮动,三列布局,高度宽度自适应

热门文章

  1. 安全 流程服务器开新机器 内外网 iptables 安全组 用户安全root用户的使用.
  2. python ascii codec can&#39;t decode
  3. mysql根据查询结果,创建表
  4. C++之函数模板
  5. Laravel5.1 搭建博客 --编译前端文件
  6. Spring.NET学习笔记——目录(原)
  7. iOS开发之--使用storyboard进行跳转
  8. &#39;not all arguments converted during string formatting&#39;错误告警信息解决办法
  9. SQL TRIM()函数去除字符串头尾空格
  10. poj_3259 负权和环