条件、循环、函数定义、字符串操作练习
1.用循环画五角星
import turtle
turtle.color('yellow')
turtle.begin_fill()
turtle.fillcolor('yellow')
for i in range(5):
turtle.forward(300)
turtle.left(144)
turtle.end_fill()
2.用循环画同心圆
import turtle for i in range(5): turtle.up() turtle.goto(0,-50*i) turtle.down() turtle.circle(50*i)
3.用while循环画太阳花
from turtle import *
color("red")
fillcolor("yellow")
begin_fill()
while True:
forward(300)
left(170)
if abs(pos())<1:
break
end_fill()
4.用函数定义画五个五角星
import turtle
turtle.bgcolor('red')
turtle.color('yellow')
turtle.fillcolor('yellow')
def my_goto(x,y):
turtle.up()
turtle.goto(x,y)
turtle.down()
def my_draw(r):
turtle.begin_fill()
for i in range(5):
turtle.forward(r)
turtle.right(144)
turtle.end_fill()
my_goto(-310,200)
my_draw(150)
my_goto(-180,290)
my_draw(40)
my_goto(-100,230)
my_draw(40)
my_goto(-100,130)
my_draw(40)
my_goto(-180,70)
my_draw(40)
5.用函数定义画钻石花瓣的太阳花
from turtle import *
color("blue")
fillcolor("pink")
def my_L():
forward(100)
right(45)
forward(100)
right(135)
forward(100)
right(45)
forward(100)
right(135)
begin_fill()
for i in range(18):
my_L()
left(20)
end_fill()
goto(0,-350)
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)

浙公网安备 33010602011771号