python實戰訓練 基礎練習 12

2021-10-08 15:53:01 字數 3509 閱讀 7301

題目 畫圖,學用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

):canvas.create_oval(

310-k,

250-k,

310+k,

250+k,width=1)

k+=j

j+=0.3mainloop(

)

題目 畫圖,學用line畫直線。

程式分析 無。

if __name__ ==

'__main__'

:from tkinter import*

canvas = canvas(width=

300, height=

300, bg=

'green'

)

canvas.pack(expand=yes, fill=both)

x0 =

263 y0 =

263 y1 =

275 x1 =

275for i in

range(19

):canvas.create_line(x0,y0,x0,y1, width=

1, fill=

'red'

) x0 = x0 -

5 y0 = y0 -

5 x1 = x1 +

5 y1 = y1 +5

x0 =

263 y1 =

275 y0 =

263for i in

range(21

):canvas.create_line(x0,y0,x0,y1,fill =

'red'

) x0 +=

5 y0 +=

5 y1 +=5

mainloop(

)

題目 畫圖,學用rectangle畫方形。

程式分析 無。

if __name__ ==

'__main__'

:from tkinter import

* root = tk(

) root.title(

'canvas'

) canvas = canvas(root,width =

400,height =

400,bg =

'yellow'

) x0 =

263 y0 =

263 y1 =

275 x1 =

275for i in

range(19

):canvas.create_rectangle(x0,y0,x1,y1)

x0 -=

5 y0 -=

5 x1 +=

5 y1 +=

5

canvas.pack(

) root.mainloop(

)

題目 畫圖,綜合例子。

程式分析 無。

if __name__  ==

'__main__'

:from tkinter import

* canvas = canvas(width =

300,height =

300,bg =

'green'

) canvas.pack(expand = yes,fill = both)

x0 =

150 y0 =

100 canvas.create_oval(x0 -

10,y0 -

10,x0 +

10,y0 +10)

canvas.create_oval(x0 -

20,y0 -

20,x0 +

20,y0 +20)

canvas.create_oval(x0 -

50,y0 -

50,x0 +

50,y0 +50)

import math

b =0.809

for i in

range(16

):a =

2* math.pi /

16* i

x = math.ceil(x0 +

48* math.cos(a)

) y = math.ceil(y0 +

48* math.sin(a)

* b)

canvas.create_line(x0,y0,x,y,fill =

'red'

) canvas.create_oval(x0 -

60,y0 -

60,x0 +

60,y0 +60)

for k in

range

(501):

for i in

range(17

):a =(2

* math.pi /16)

* i +(2

* math.pi /

180)

* k x = math.ceil(x0 +

48* math.cos(a)

) y = math.ceil(y0 +

48+ math.sin(a)

* b)

canvas.create_line(x0,y0,x,y,fill =

'red'

)for j in

range(51

):a =(2

* math.pi /16)

* i +(2

* math.pi /

180)

* k -

1 x = math.ceil(x0 +

48* math.cos(a)

) y = math.ceil(y0 +

48* math.sin(a)

* b)

canvas.create_line(x0,y0,x,y,fill =

'red'

) mainloop(

)

題目 計算字串長度。

程式分析 無。

s=

'zhangguang101'

print

(len

(s))

python實戰訓練 基礎練習 13

題目 列印出楊輝三角形前十行。程式分析 無。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 ...

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