条件、循环、函数定义、字符串操作练习
1.用循环画五角星
import turtle turtle.color("red") turtle.fillcolor("red") turtle.begin_fill() for i in range(5): turtle.forward(200) turtle.right(144) turtle.end_fill()
2.用循环画同心圆
import
turtle
for
i
in
range
(
5
):
turtle.up()
turtle.goto(
0
,
-
10
*
(i
+
1
))
turtle.down()
turtle.circle(
10
*
(i
+
1
))
3.用while循环画太阳花
from turtle import * color('red','yellow') begin_fill() while True: forward(200) left(170) if abs(pos())<1: break end_fill()
4.用函数定义画五个五角星
import
turtle
turtle.setup(
600
,
400
,
0
,
0
)
turtle.color(
"yellow"
)
turtle.bgcolor(
'red'
)
turtle.fillcolor(
"yellow"
)
def
xy(x,y):
turtle.up()
turtle.goto(x,y)
turtle.down()
def
draw(r):
turtle.begin_fill()
for
i
in
range
(
5
):
turtle.forward(r)
turtle.right(
144
)
turtle.end_fill()
xy(
-
273
,
110
)
draw(
100
)
xy(
-
121
,
162
)
turtle.left(
45
)
draw(
50
)
xy(
-
79
,
110
)
draw(
50
)
xy(
-
79
,
51
)
turtle.right(
45
)
draw(
50
)
xy(
-
121
,
22
)
turtle.right(
45
)
draw(
50
)
5.用函数定义画钻石花瓣的太阳花
import turtle def draw_diamond(brad): brad.forward(120) brad.right(45) brad.forward(120) brad.right(135) def draw_art(): window=turtle.Screen() window.bgcolor("pink") brad=turtle.Turtle() brad.shape("turtle") brad.color("red") for i in range(1,13): draw_diamond(brad) draw_diamond(brad) brad.left(30) window.exitonclick() draw_art()
6.输入学号,识别年级、专业、序号
sid=input("学号:") g=sid[2:4] p=sid[4:8] t=sid[-2:] print("年级:{}级".format(g)) if p=="0611": print("专业:网络工程") print("序号:{}".format(t))
7.输入1-7的数字,输出对应的“星期几”。
s="星期一星期二星期三星期四星期五星期六星期日" t=int(input("1-7:")) print(s[3*(t-1):3*t])
8.识别身份证号中的省市区、年龄、性别
id=input("身份证号:") s=id[0:5] q=id[14:17] t=id[6:10] if s=="440682": print("地区:佛山") if int(q)%2==0: print("女") else: print("男") a=2017-int(t) print("年龄:{}".format(a))
9.用字符串操作生成python文档各库的网址(起始网址在这里https://docs.python.org/3.6/library/index.html)
s="https://docs.python.org/3.6/library/index" t=".html" add=s+t print(add)