首页 > 代码库 > 写过的小程序

写过的小程序

生成200个10位的随机数(生成优惠券):
__author__ = ‘friday‘
import random

def creat_num(num,long):
    str = ‘qwertyuiopasdfghjklzxcvbnm1234567890!@#$%^&*_+‘
    b = []
    for i in range(num):
        a = ‘‘
        for j in range(long):
            a += random.choice(str)
        b.append(a)
    for i in range(len(b)):
        print(b[i])

if __name__ == ‘__main__‘:
    creat_num(200,10) 

判断有多少个英文单词:
import re
from collections import Counter


def word_count(txt):
    word_pattern = r‘[a-zA-Z-]+‘
    words = re.findall(word_pattern, txt)
    return Counter(words).items()

if __name__ == ‘__main__‘:
    txt = open(‘D:\\f.txt‘, ‘r‘).read().lower()
    print word_count(txt)


为头像加数字:
__author__ = ‘Kxrr‘

from PIL import Image,ImageDraw,ImageFont
import random

msgNum = str(random.randint(1,99))

# Read image
im = Image.open(‘D:\\1.jpg‘)
w,h = im.size
wDraw = 0.8 * w
hDraw = 0.08 * w

# Draw image
font = ImageFont.truetype(‘D:\\1.ttc‘, 30) # use absolute font path to fix ‘IOError: cannot open resource‘
draw = ImageDraw.Draw(im)
draw.text((wDraw,hDraw), msgNum, font=font, fill=(255,33,33))

# Save image
im.save(‘D:\\kxrr_msg.png‘, ‘png‘) 

有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
import os
import re

def find_path(path):
    c = 0
    for i in os.listdir(path):
        py=os.path.join(path,i)
        a=open(py)
        commentline = 0
        blankline = 0
        lines = len(a.readlines())
        print "There are %d lines in %s" % (lines, py)
        for ii in a.readlines():   
            pattern = re.compile(r‘(\s*)#‘)
            pattern1 = re.compile(r‘(\s*)$‘)
            if pattern.match(ii):
                    commentline += 1
            if pattern1.match(ii):
                    blankline += 1
        print "blankline is:",blankline,"commentline is:",commentline

find_path(‘d://1‘)







来自为知笔记(Wiz)


写过的小程序