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)))