条件、循环、函数定义、字符串操作练习
注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
对前面的代码进行优化,用for,while,if,def实现:
1:用循环画五角星
1 import turtle 2 3 turtle.fillcolor("red") 4 turtle.begin_fill() 5 for i in range(5): 6 turtle.forward(100) 7 turtle.right(144) 8 turtle.end_fill()
2:用循环画同心圆
1 import turtle 2 for i in range(3): 3 turtle.up() 4 turtle.goto(0,-50*(i+1)) 5 turtle.down() 6 turtle.circle(50*(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()
4:用函数定义画五个五角星
1 import turtle 2 turtle.setup(600,400,0,0) 3 turtle.color("yellow") 4 turtle.bgcolor('red') 5 turtle.fillcolor("yellow") 6 7 def Mygoto(x,y): 8 turtle.up() 9 turtle.goto(x,y) 10 turtle.down() 11 12 def Mydraw(r): 13 turtle.begin_fill() 14 for i in range(5): 15 turtle.forward(r) 16 turtle.right(144) 17 turtle.end_fill() 18 19 Mygoto(-273,110) 20 Mydraw(100) 21 22 Mygoto(-121,162) 23 turtle.left(45) 24 Mydraw(50) 25 26 Mygoto(-79,110) 27 Mydraw(50) 28 29 Mygoto(-79,51) 30 turtle.right(45) 31 Mydraw(50) 32 33 Mygoto(-121,22) 34 turtle.right(45) 35 Mydraw(50)
5:用函数定义画钻石花瓣的太阳花
1 import turtle 2 def Yyxdraw_diamond(brad): 3 brad.forward(120) 4 brad.right(45) 5 brad.forward(120) 6 brad.right(135) 7 def Yyxdraw_art(): 8 window=turtle.Screen() 9 window.bgcolor("red") 10 brad=turtle.Turtle() 11 brad.shape("turtle") 12 brad.color("yellow") 13 for i in range(36): 14 Yyxdraw_diamond(brad) 15 Yyxdraw_diamond(brad) 16 brad.left(10) 17 window.exitonclick() 18 19 Yyxdraw_art()
字符串操作
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的数字,输出对应的“星期几”。
1 s="星期一星期二星期三星期四星期五星期六星期天" 2 i=int(input("请输入(1-7):")) 3 if(0<i<8): 4 print(s[-3+3*i:0+3*i]) 5 else: 6 print("输入有误!")
3:识别身份证号中的省市区、年龄、性别。
1 ID=input('请输入十八位身份证号码: ') 2 if len(ID)!=18: 3 print("错误的身份证号码!!") 4 5 ID_add=ID[0:6] 6 ID_birth=ID[6:10] 7 ID_sex=ID[14:17] 8 9 if int(ID_add)==440101: 10 print("省市区:广东省广州市市辖区") 11 elif int(ID_add)==440102: 12 print("省市区:广东省广州市东山区") 13 elif int(ID_add)==440103: 14 print("省市区:广东省广州市荔湾区") 15 elif int(ID_add)==440104: 16 print("省市区:广东省广州市越秀区") 17 elif int(ID_add)==440105: 18 print("省市区:广东省广州市海珠区") 19 20 21 birth=2017-int(ID_birth[0:4]) 22 print("年龄:{}".format(birth)) 23 24 if int(ID_sex)%2==0: 25 print('性别:女') 26 else: 27 print('性别:男')
4:用字符串操作生成python文档各库的网址(起始网址在这里https://docs.python.org/3.6/library/index.html)
1 yyx1 = "https://docs.python.org/3.6/library/" 2 yyx2 = ".html" 3 yyx3 = input("请输入库名:") 4 print("网址为:%s%s%s"%(yyx1,yyx3,yyx2))
5:练习字符串的+,*,in,len(),eval()
1 print("No!"*3) 2 s="this is " 3 t="younger!" 4 print(s+t) 5 p="hello world!!!" 6 for i in range(len(s)): 7 print(i,s[i])