python實戰訓練 基礎練習 13

2021-10-08 17:17:41 字數 3247 閱讀 3874

題目 :

列印出楊輝三角形前十行。

程式分析 無。

def

generate

(numrows)

: r =[[

1]]for i in

range(1

,numrows)

:list

(map

(lambda x,y:x+y,[0

]+r[-1

],r[-1

]+[0

])))

return r[

:numrows]

a=generate(10)

for i in a:

print

(i)

題目:

查詢字串。

程式分析 無。

s1=

'aabbxuebixuebi'

s2='ab'

s3='xue'

print

(s1.find(s2)

)print

(s1.find(s3)

)

題目 畫橢圓。

程式分析 使用 tkinter。

if __name__ ==

'__main__'

:from tkinter import

* x =

360 y =

160 top = y -

30 bottom = y -

30

canvas = canvas(width =

400,height =

600,bg =

'white'

)for i in

range(20

):canvas.create_oval(

250- top,

250- bottom,

250+ top,

250+ bottom)

top -=

5 bottom +=

5 canvas.pack(

) mainloop(

)

題目 利用ellipse 和 rectangle 畫圖。。

程式分析 無。

if __name__ ==

'__main__'

:from tkinter import

* canvas = canvas(width =

400,height =

600,bg =

'white'

) left =

20 right =

50 top =

50 num =

15for i in

range

(num)

: canvas.create_oval(

250- right,

250- left,

250+ right,

250+ left)

canvas.create_oval(

250-20,

250- top,

250+20,

250+ top)

canvas.create_rectangle(20-

2* i,20-

2* i,10*

(i +2)

,10*( i +2)

) right +=

5 left +=

5 top +=

10 canvas.pack(

) mainloop(

)

題目 乙個最優美的圖案。

程式分析 無。

import math

from tkinter import

*class

pts:

def__init__

(self)

: self.x =

0 self.y =

0points =

deflinetodemo()

: screenx =

400 screeny =

400 canvas = canvas(width = screenx,height = screeny,bg =

'white'

) aspectratio =

0.85

maxpts =

15 h = screeny

w = screenx

xcenter = w /

2 ycenter = h /

2 radius =

(h -30)

/(aspectratio *2)

-20step =

360/ maxpts

angle =

0.0for i in

range

(maxpts)

: rads = angle * math.pi /

180.0

p = pts(

) p.x = xcenter +

int(math.cos(rads)

* radius)

p.y = ycenter -

int(math.sin(rads)

* radius * aspectratio)

angle += step

canvas.create_oval(xcenter - radius,ycenter - radius,

xcenter + radius,ycenter + radius)

for i in

range

(maxpts)

:for j in

range

(i,maxpts)

: canvas.create_line(points[i]

.x,points[i]

.y,points[j]

.x,points[j]

.y) canvas.pack(

) mainloop(

)if __name__ ==

'__main__'

: linetodemo(

)

python實戰訓練 基礎練習 12

題目 畫圖,學用circle畫圓形。程式分析 無。from tkinter import canvas canvas width 800,height 600,bg yellow canvas.pack expand yes,fill both k 1 j 1for i in range 26 ca...

python實戰訓練 基礎練習 14

題目 輸入3個數a,b,c,按大小順序輸出。程式分析 同例項005。raw for i in range 3 x int input int d i for i in range len raw for j in range i,len raw if raw i raw j raw i raw j ...

python實戰訓練 基礎練習 16

題目 編寫乙個函式,輸入n為偶數時,呼叫函式求1 2 1 4 1 n,當輸入n為奇數時,呼叫函式1 1 1 3 1 n 程式分析 無。def peven n i 0 s 0.0for i in range 2 n 1,2 s 1.0 i return s defpodd n s 0.0for i i...