### 條件、循環、函數定義、字符串操作練習

1.用循環畫五角星

`from turtle import *color('yellow','yellow')begin_fill()while True:    forward(100)    right(144)    if abs(pos())<1:        breakend_fill()`

2.用循環畫同心圓

`import turtlefor i in range(2,10):    turtle.up()    turtle.goto(0,-20*i)    turtle.down()    turtle.circle(20*i)    `

3.用while循環畫太陽花

`from turtle import *color('green','white')begin_fill()while True:    forward(200)    left(170)    if abs(pos())<1:        breakend_fill()done()`

4.用函數定義畫五個五角星

`from turtle import *setup(600,400)bgcolor('red')color('yellow')def h_draw(r):    begin_fill()    for i in range(5):        forward(r)        right(144)    end_fill()def h_goto(x,y,z):    up()    goto(x,y)    setheading(z)    down()h_goto(-260,120,0)h_draw(120)h_goto(-110,160,40)h_draw(40)h_goto(-65,125,10)h_draw(40)h_goto(-55,55,40)h_draw(40)h_goto(-110,15,20)h_draw(40)`

5.用函數定義畫鑽石花瓣的太陽花

`import turtleturtle.color('red')turtle.fillcolor('yellow')turtle.begin_fill()def h_draw(t):    t.fd(80)    t.right(45)    t.fd(80)    t.right(135)for i in range(36):    h_draw(turtle)    h_draw(turtle)    turtle.left(10)turtle.end_fill()turtle.right(90)turtle.pensize(5)turtle.color('green')turtle.forward(300)`

6.字符串操作

a.輸入學號，識別年級、專業、序號

`a=input('請輸入你的學號：')print('你的年級是{}級'.format(a[2:4]))print('你的專業序號是{}'.format(a[8:10]))print('你的班級學號是{}'.format(a[10:]))`

b.輸入1-7的數字，輸出對應的“星期幾”

`s="星期一星期二星期三星期四星期五星期六星期天"i=int(input("請輸入(1-7)："))if(0<i<8):    print(s[-3+3*i:0+3*i])else:    print("輸入有誤!")`

c.識別身份證號中的省市區、年齡、性別

`ID=input('請輸入十八位身份證號碼: ')if len(ID)!=18:  print("錯誤的身份證號碼!!")   ID_add=ID[0:6]ID_birth=ID[6:10]ID_sex=ID[14:17] if int(ID_add)==440101:  print("省市區:廣東省廣州市市轄區")elif int(ID_add)==440102:    print("省市區:廣東省廣州市東山區")elif int(ID_add)==440103:    print("省市區:廣東省廣州市荔灣區")elif int(ID_add)==440104:    print("省市區:廣東省廣州市越秀區")elif int(ID_add)==440105:    print("省市區:廣東省廣州市海珠區")      birth=2017-int(ID_birth[0:4])print("年齡：{}".format(birth))    if int(ID_sex)%2==0:  print('性別：女')else:  print('性別：男')`

d.用字符串操作生成python文檔各庫的網址(起始網址在這里https://docs.python.org/3.6/library/index.html)

`a="https://docs.python.org/3.6/library/index"b=".html"address=a+bprint(address)`

e.練習字符串的+,*,in,len(),eval()