标签:blog bre end else 菱形 others git while循环 ade
一、注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
二、对前面的代码进行优化,用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 import turtle 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()
标签:blog bre end else 菱形 others git while循环 ade
原文地址:http://www.cnblogs.com/young27/p/7519815.html