【作業】條件、循環、函數定義、字符串操作練習
阿新 • • 發佈:2017-09-14
同心圓 print com alt while imp val orm format
一、註意標準庫的兩種導入與使用方式,建議大家采用<庫名>.<函數名>的方式。
二、對前面的代碼進行優化,用for,while,if,def實現:
1、用循環畫五角星
1 from turtle import* 2 color("red") 3 fillcolor("red") 4 begin_fill() 5 while True: 6 forward(200) 7 right(144) 8 if abs(pos())<1: 9 break 10 end_fill()
2、用循環畫同心圓
1 importturtle 2 for i in range(3): 3 turtle.up() 4 turtle.goto(0,-20*(i+1)) 5 turtle.down() 6 turtle.circle(20*(i+1))
3、用while循環畫太陽花
1 import turtle 2 turtle.color(‘red‘,‘yellow‘) 3 turtle.begin_fill() 4 while True: 5 turtle.forward(200) 6 turtle.left(170) 7 if abs(turtle.pos())<1:8 break 9 turtle.end_fill() 10 done()
4、用函數定義畫五個五角星
1 def mystar(z): 2 turtle.begin_fill() 3 for i in range(5): 4 turtle.forward(z) 5 turtle.right(144) 6 turtle.end_fill() 7 8 def otherstart(): 9 turtle.begin_fill() 10 for i in range(5): 11 turtle.right(144)12 turtle.forward(35) 13 turtle.end_fill() 14 15 mygoto(-250,125) 16 mystar(125) 17 18 mygoto(-105,165) 19 turtle.left(40) 20 turtle.begin_fill() 21 for i in range(5): 22 turtle.forward(35) 23 turtle.right(144) 24 turtle.end_fill() 25 26 mygoto(-30,135) 27 turtle.right(10) 28 turtle.begin_fill() 29 for i in range(5): 30 turtle.right(144) 31 turtle.forward(35) 32 turtle.end_fill() 33 34 mygoto(-40,75) 35 turtle.left(40) 36 turtle.begin_fill() 37 for i in range(5): 38 turtle.right(144) 39 turtle.forward(35) 40 turtle.end_fill() 41 42 mygoto(-75,20) 43 turtle.right(20) 44 turtle.begin_fill() 45 for i in range(5): 46 turtle.right(144) 47 turtle.forward(35) 48 turtle.end_fill()
5、用函數定義畫鉆石花瓣的太陽花
1 from turtle import * 2 def draw_prismatic(): 3 for i in range(1,3): #要轉三次方向來畫一個菱形 4 forward(100) 5 right(45) 6 forward(100) 7 right(135) 8 9 def draw_Diamond_Flower(): 10 speed(10) 11 color(‘red‘,‘yellow‘) 12 begin_fill() 13 for i in range(36): 14 draw_prismatic() 15 right(10) 16 end_fill() 17 18 draw_Diamond_Flower()
三、字符串操作
1、輸入學號,識別年級、專業、序號。
1 majors = { 2 11:‘網絡工程‘, 3 12:‘軟件工程‘, 4 13:‘數字媒體‘, 5 } 6 7 def recognition(studentID): 8 if len(studentID)<12: 9 print("請輸入正確的學號!") 10 elif studentID.isdigit() != True: 11 print("請輸入正確的學號!") 12 else: 13 grade = studentID[0:4] 14 major = studentID[6:8] 15 num = studentID[10:12] 16 print("年級:{}級".format(grade)) 17 print("專業為:",majors.get(int(major))) 18 print("序號:{}".format(num)) 19 20 studentID = input("請輸入學號:") 21 recognition(studentID)
2、輸入1-7的數字,輸出對應的“星期幾”。
3、識別身份證號中的省市區、年齡、性別。
4、用字符串操作生成python文檔各庫的網址(起始網址在這裏https://docs.python.org/3.6/library/index.html)
5、練習字符串的+,*,in,len(),eval()
【作業】條件、循環、函數定義、字符串操作練習