1. 求简洁优美的python代码例子、片段、参考资料
楼主贴的那段代码好像是我写的那段吧,我来告诉你如何写出来的吧
首先我不是高手,我也没有人教,我的编程都是自学的,我只是一个业余爱好者.
写出这样的代码很简单,就是要多练,我只是把python的基本语法学会,然后就不停地练习,我没有看过楼上的那些资料,我只是不停地码代码,或许有捷径,但是我没有发现.
我从07年开始写python的脚本,我一开始的代码风格也很差,特别是我先学c++,然后再转python的,当写的代码越来越多,对python的了解就会加深,代码风格也会自动改变的,不需要着急,其实这就是对一门语言的了解程度,你可以看看我回答的问题,我的回答就是我对python的理解,如果你能坚持下来,相信7年后你写的代码会比我写得更好.
楼上的题目有点意思,我也写一下,不知道对否
s='''
TheZenofPython,byTimPeters
Beautifulisbetterthanugly.
Explicitisbetterthanimplicit.
Simpleisbetterthancomplex.
.
Flatisbetterthannested.
Sparseisbetterthandense.
Readabilitycounts.
Specialcasesaren'tspecialenoughtobreaktherules.
.
Errorsshouldneverpasssilently.
Unlessexplicitlysilenced.
Inthefaceofambiguity,refusethetemptationtoguess.
Thereshouldbeone--andpreferablyonlyone--obviouswaytodoit.
'reDutch.
Nowisbetterthannever.
*right*now.
,it'sabadidea.
,itmaybeagoodidea.
--let'sdomoreofthose!
'''
importre,collections
tail_map={"'s":'is',"'re":'are',"n't":'not'}
data=collections.Counter(re.findall('w+',re.sub("('s|'re|n't)",lambdamatchobj:tail_map[matchobj.group()],s.lower())))
max_len=max(data.values())
print('Totalwordcount:%d',sum(data.values()))
forwordinsorted(data):
print('%*s=>%d'%(max_len,word,data[word]))
2. python有趣的编程代码
classPoint:
row=0
col=0
def__init__(self,row,col):
self.row=row
self.col=col
def(self):
returnPoint(row=self.row,col=self.col)
#初始框架
importpygame
importrandom
#初始化
pygame.init()
W=800
H=600
ROW=30
COL=40
size=(W,H)
window=pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
bg_color=(255,255,255)
snake_color=(200,200,200)
head=Point(row=int(ROW/2),col=int(COL/2))
head_color=(0,128,128)
snakes=[
Point(row=head.row,col=head.col+1),
Point(row=head.row,col=head.col+2),
Point(row=head.row,col=head.col+3)
]
#生成食物
defgen_food():
while1:
pos=Point(row=random.randint(0,ROW-1),col=random.randint(0,COL-1))
#
is_coll=False
#是否跟蛇碰上了
ifhead.row==pos.rowandhead.col==pos.col:
is_coll=True
#蛇身子
forsnakeinsnakes:
ifsnake.row==pos.rowandsnake.col==pos.col:
is_coll=True
break
ifnotis_coll:
break
returnpos
#定义坐标
food=gen_food()
food_color=(255,255,0)direct='left'#left,right,up,down
#
defrect(point,color):
cell_width=W/COL
cell_height=H/ROW
left=point.col*cell_width
top=point.row*cell_height
pygame.draw.rect(
window,color,
(left,top,cell_width,cell_height)
)
pass
#游戏循环
quit=True
clock=pygame.time.Clock()
whilequit:
#处理事件
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
quit=False
elifevent.type==pygame.KEYDOWN:
ifevent.key==273orevent.key==119:
ifdirect=='left'ordirect=='right':
direct='up'
elifevent.key==274orevent.key==115:
ifdirect=='left'ordirect=='right':
direct='down'
elifevent.key==276orevent.key==97:
ifdirect=='up'ordirect=='down':
direct='left'
elifevent.key==275orevent.key==100:
ifdirect=='up'ordirect=='down':
direct='right'
#吃东西
eat=(head.row==food.rowandhead.col==food.col)
#重新产生食物
ifeat:
food=gen_food()
#处理身子
#1.把原来的头,插入到snakes的头上
snakes.insert(0,head.())
#2.把snakes的最后一个删掉
ifnoteat:
snakes.pop()
#移动
ifdirect=='left':
head.col-=1
elifdirect=='right':
head.col+=1
elifdirect=='up':
head.row-=1
elifdirect=='down':
head.row+=1
#检测
dead=False
#1.撞墙
ifhead.col<0orhead.row<0orhead.col>=COLorhead.row>=ROW:
dead=True
#2.撞自己
forsnakeinsnakes:
ifhead.col==snake.colandhead.row==snake.row:
dead=True
break
ifdead:
print('死了')
quit=False
#渲染——画出来
#背景
pygame.draw.rect(window,bg_color,(0,0,W,H))
#蛇头
forsnakeinsnakes:
rect(snake,snake_color)
rect(head,head_color)
rect(food,food_color)
#
pygame.display.flip()
#设置帧频(速度)
clock.tick(8)
#收尾工作
这是一个简易版贪吃蛇的代码,虽然结构简单,但是该有的功能都是完整的,可玩性也不错
3. 新手求教一个简单的python代码!
k=0
whilek>=0:
if5**(3**k)%2==3:
print(k)
break
k+=1
4. 写一个20行以上的python简单代码
classPrice:
ticket_d=100
ticket_w=ticket_d*1.2
defrq(self):
self.a=int(input('请输入是平日还是周末(平日:1/周末:0):'))
ifself.a==0:
self.p=self.ticket_w
ifself.a==1:
self.p=self.ticket_d
defpj(self):
input('请输入是人数'+' ')
self.ad=int(input('大人数量:'))
self.ch=int(input('儿童数量:'))
self.money=self.p*self.ad+self.p*self.ch/2
print('%f'%self.money)
classTicket():
def__init__(self,weekend=False,child=False):
self.exp=100
ifweekend:
self.inc=1.2
else:
self.inc=1
ifchild:
self.discount=0.5
else:
self.discount=1
defcalcPrice(self,num):
returnself.exp*self.inc*self.discount*num
alt=Ticket()
child=Ticket(child=True)
print("2个成人+1个小孩平日票价为:%.2f"%(alt.calcPrice(2)+child.calcPrice(1)))