大牛更新的练习题,说好的持续更新呢。以自己目前学到的知识点写出来的代码,新人可以看看
http://www.nnzhp.cn/archives/433
练习1:
产生的账号是以@163.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大写字母、小写字母、数字组成
import string,random lenth = input("请输入用户名的长度:") num = input("请输入产生用户的条数:") namelist = [] count = 0 with open('name.txt','a+') as f: while count < int(num): lowletter = random.sample(string.ascii_lowercase,1)#返回一个列表 upletter = random.sample(string.ascii_uppercase,1) numletter = random.sample(string.digits,(int(lenth)-2)) tmp = lowletter+upletter+numletter random.shuffle(tmp) name = ''.join(tmp) if name in namelist:#用户名不能重复 continue else: namelist.append(name) count +=1 f.write(name+'@163.com\n')
练习2:
公司服务器,经常被别人攻击,要写个监控nginx日志的脚本,每分钟运行一次,如果这一分钟内同一个ip请求次数超过100次,加入黑名单
日志文件格式如下:
178.210.90.90 - - [04/Jun/2017:03:44:13 +0800] "GET /wp-includes/logo_img.php HTTP/1.0" 302 161 "http://nnzhp.cn/wp-includes/logo_img.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "10.3.152.221" 178.210.90.90 - - [04/Jun/2017:03:44:13 +0800] "GET /blog HTTP/1.0" 301 233 "http://nnzhp.cn/wp-includes/logo_img.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "10.3.152.221" 178.210.90.90 - - [04/Jun/2017:03:44:15 +0800] "GET /blog/ HTTP/1.0" 200 38278 "http://nnzhp.cn/wp-includes/logo_img.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "10.3.152.221" 66.249.75.29 - - [04/Jun/2017:03:45:55 +0800] "GET /bbs/forum.php?mod=forumdisplay&fid=574&filter=hot HTTP/1.1" 200 17482 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
import time point = 0 while 1: with open('access.log','r') as f: f.seek(point) ipList = {} #存放ip和出现的次数 # ipList = []#回家用存list方式再写一遍********************** for line in f: ip = line.split()[0] # print(ip) if ip in ipList: ipList[ip] += 1 else: ipList[ip] = 1 # print(ipList) point = f.tell() for k in ipList: if ipList.get(k)>=50: print('该ip:%s在攻击你'%k) # # for i in ipList: # if ipList.count(i) >=100: # heike.append(i) # print('该ip:%s在攻击你' % i) time.sleep(60)#程序停止60s(此时平台获取60s的日志)
练习3:
额,来个小插曲,写程序求出1-100的累加和。1+2+3+4+5+.....+100
sum = 0 for i in range(101): sum+=i print(sum)
练习4:
不知不觉你已经写了很多python代码了,代码全部都放在e盘下面的my_code文件夹中,突然突发奇想,想统计了一下总共写了多少行代码包括空行和注释,要把代码的行数、空行数、注释行数都统计出来。
spaceCount = 0 #空格行数 zCount = 0 #注释行数 cCount = 0 #代码行数 start_index = False #多行注释是否开始 with open('高效循环字典的方式.py','r',encoding='utf-8') as f: for i,line in enumerate(f,1): if not start_index: if line.strip().startswith("'''") or line.strip().startswith('"""'): start_index = True startline = i elif not line.strip(): spaceCount+=1 elif line.strip()[0] == '#': zCount+=1 else: cCount+=1 else: if line.strip().startswith("'''") or line.strip().startswith('"""'): endline = i zCount += endline - startline+1 start_index = False #多行注释结束,标志位恢复 print('总代码行数:',i) print('空行数:',spaceCount) print('注释行:',zCount)
练习5:
有一个文件,里面有一些敏感词汇,如下,如果输入这些词,就用**代替,然后输出,例如输入今天没吃饭,碰到一个傻逼,原来那个sb是小明。输出今天没吃饭,碰到一个**,原来那个**是小明。
word.txt的内容如下:
SB
傻逼
煞笔
shabi
傻b
傻B
煞比
sb
str = input('请输入:') with open('word.txt','r',encoding='utf-8') as f: for i in f: i = i.strip() if i.strip() in str: str = str.replace(i.strip(),'***') print(str)
练习6:
python可不引入第三方变量就可交换两个变量的值
a=1 b=2 a,b = b,a print(a,b)
练习7:
现有字符串a="Im Love python",需要变成b="python love Im"
a = "Im Love python" alist = a.split() print(alist) alist.reverse() print(' '.join(alist))
练习8:
将"wdnwjfwj we hd 9#sdfekfmew 2011"里的数字取出来相加结果为2020
str = "wdnwjfwj we hd 9#sdfekfmew 2011" list = str.split() re = int(list[3][0])+int(list[4]) print(re)
练习9:
下面有两个队伍,,a不和x对战,b不和y,z,请写代码实现
team1 = ['a','b','c'] team2 = ['x','y','z'] for i in range(len(team1)): for j in range(len(team2)): if team1[i]=='a' and team2[j]=='x': pass elif team1[i]=='b' and team2[j]== 'y': pass elif team1[i]=='b' and team2[j]== 'z': pass else: print(team1[i],' VS ',team2[j])
练习10:
使用for循环输出1 2 3 4 5 6 8 9 10
for i in range(10): if i == 6: pass else: i += 1 print(i,end=' ')
输出 1-100 内的所有奇数
for i in range(100): if i%2!=0: print(i,end=' ')
输入quit退出循环
while 1: text = input('请输入:') if text == 'quit': print('退出成功') break else: continue