哪位大神帮忙Python编程!! python作品代码
python求大神!
Well, I'm a python 2.7 user, and graphics should be only available after 3.x as I googled~
But I found that pygame is a pretty lib for graphcis also.
So, after scratching my head for one hour with pygame, I got this little stuff for you.
And as I see on python 3.0 ducumentation, graphics seems easier to use than pygame,
so come on dude, it's not that hard, just do it!
#
from pygame.locals import Rect
import sys
pygame.init()
width = 500
height = 350
window = pygame.display.set_mode((width, height))
color = (255, 255, 255)
pygame.display.flip()
mouse_cnt = 0
house = []
flen = 0
lp, rp = 0, 0 #door
def lineLeng(pos1, pos2):
x = pos1[0]-pos2[0]
y = pos1[1]-pos2[1]
return int((x**2+y**2)**0.5)
def door(pos, flen):
dLen = flen/5
return (pos[0]-dLen, pos[1]), (pos[0]+dLen, pos[1])
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.MOUSEBUTTONUP and mouse_cnt < 5:
mouse_cnt+=1
house.append(pygame.mouse.get_pos())
if mouse_cnt == 2:
pygame.draw.line(window, color, house[0], house[1])
flen = lineLeng(house[0], house[1])
if mouse_cnt == 3:
lp, rp = door(house[2], flen)
pygame.draw.line(window, color, lp, rp)
if mouse_cnt == 4:
wul = (house[3][0]-flen/20, house[3][1]-flen/20)
wbr = (house[3][0]+flen/20, house[3][1]+flen/20)
print wul,wbr, flen/20
pygame.draw.rect(window, color, Rect(wul, (abs(wbr[0]-wul[0]),abs(wbr[1]-wul[1]))))
if mouse_cnt == 5:
pygame.draw.line(window, color, house[0], house[4])
pygame.draw.line(window, color, house[1], house[4])
pygame.draw.line(window, color, lp, (lp[0], lp[1]+30))
pygame.draw.line(window, color, rp, (rp[0], rp[1]+30))
pygame.draw.line(window, color, house[0], (house[0][0], lp[1]+30))
pygame.draw.line(window, color, house[1], (house[1][0], rp[1]+30))
pygame.display.update()
菜鸟求python代码 求各位大神帮忙解答下
with open('1.txt','r') as f:
a=f.readlines()
b=[]
for i in a :
i=int(i)
if i<=100 and i>=90 :
b.append(str(i)+":A")
elif i>=80:
b.append(str(i)+":B")
elif i>=70:
b.append(str(i)+":C")
elif i>=60:
b.append(str(i)+":D")
else :
b.append(str(i)+":E")
print(b)
请问诸位python编程大师:
a='banana'print a.replace('an','',1)b='bicycle'print b.replace('cyc','',1)c='Mississippi'print c.replace('iss','',1)d='bicycle'print d.replace('egg','',1)这种串操作基本都有 不必重写
求高手帮写个简单的python程序~~
def cal(input="input.txt",output="output.txt"):
#cal方法为主程序,推荐这样做而不是PYTHON.EXE xx.py xxx xxx。默认参数为python目录的两个txt,如为其他文件自己指定。
infile = file(input,"r")
#打开源数据文件
outfile = file(output,"w")
#打开目标文件
linedata = "linedata"
#初始化存储每行数据的变量
status = {}
#初始化处理的数据空间。统计的数据以字典形式存储。字典的key为人的名字,value为一个2维列表,前一个数为[发送次数,发送字节]的列表嵌套,后一个数为[接受次数,接受字节]的列表嵌套。
writedata = ""
#初始化最后准备写入的数据
while True :
linedata = infile.readline() #读取一行数据
data = linedata[:-1].split(",") #以“,”为分割符(如为其他符号自己改)
if linedata == "":
break #如果行数据为空(到文件末尾了)则跳出循环
data[2]=int(data[2]) #将“传输字节”转化为数字
if not status.has_key(data[0]):
status[data[0]] = [[1,data[2]],[0,0]] #检验如果不存在发送人名字则新建一个key,其value为[1(发送一次),发送字节数],[0(接受0次),0(发送0字节)]
else :
status[data[0]][0][1] += data[2]
status[data[0]][0][0] += 1
#如存在人名,则发送次数+1,发送字节数相加
if not status.has_key(data[1]):
status[data[1]] = [[0,0],[1,data[2]]]
else :
status[data[1]][1][1] += data[2]
status[data[1]][1][0] += 1
#这个是接受次数和接受字节数的统计
for (name,[outstatus,instatus]) in status.items() :
writedata += name+","+str(outstatus[0])+","+str(outstatus[1])+","+str(instatus[0])+","+str(instatus[1])+"\n"
#准备写入的数据,包括 名字 逗号 发送次数 逗号 发送字节 逗号 接受次数 逗号 接受字节 换行符(如要其它输出格式自己改)
outfile.write(writedata) #将数据写入目标文件
infile.close()
outfile.close() #关闭文件
调用方法为(假设文件名为 status.py)。在python里面:
〉〉〉import status #载入status模块
〉〉〉status.cal() #调用cal方法
如果你还想用你原来的调用方法,就在cal方法下面加入以下内容
if __name__=='__main__':
cal()
#在windows下输入 python.exe status就可以使用了