条件、循环、函数定义、字符串操作练习

一、标准数据库两种导入方式

a.第一种

import turtle
turtle.color("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
for i in range(5):
     turtle.forward(200)
     turtle.right(144)
turtle.end_fill()

b.第二种

from turtle import*
color("yellow")
fillcolor("yellow")
begin_fill()
for i in range(5):
     forward(200)
     right(144)
end_fill()

 

二、

a.循环语句画五角星

>>> import turtle
>>> for i in range(5):
    turtle.forward(200)
    turtle.right(144)

b.循环语句画同心圆

>>> import turtle
>>> for i in range(4):
    turtle.penup()
    turtle.goto(0,-50*i)
    turtle.pendown()
    turtle.circle(50*i)

c.while循环画太阳花

 

import turtle
turtle.color('red','yellow')
turtle.begin_fill()
while True:
    turtle.forward(200)
    turtle.left(170)
    if abs(turtle.pos())<1:
        break
turtle.end_fill()
turtle.done()

d.用函数定义画五个五角星

import turtle
turtle.setup(600,400,0,0)
turtle.color("yellow")
turtle.bgcolor("red")
turtle.fillcolor("yellow")

def my_goto(x,y):
  turtle.up()
  turtle.goto(x,y)
  turtle.down()

def my_draw5(r):
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(r)
        turtle.right(144)
    turtle.end_fill()


my_goto(-250,75)
my_draw5(100)

my_goto(-100,150)
my_draw5(50)

my_goto(-60,100)
my_draw5(50)

my_goto(-90,50)
my_draw5(50)

my_goto(-120,0)
my_draw5(50)

e.用函数定义画钻石花瓣的太阳花

import turtle
turtle.color("red")
turtle.fillcolor("yellow")
turtle.begin_fill()
def draw(t):
   t.fd(80)
   t.right(45)
   t.fd(80)
   t.right(135)
for i in range(36):
    draw(turtle)
    draw(turtle)
    turtle.left(10)
turtle.end_fill()

 

 三、字符串操作

a.输入学号,识别年级、专业、序号

id = input("请输入学生的学号:")
print("该生年级为:%s 专业为:%s 班级学号为:%s"%(id[0:4],id[4:8],id[8:]))

b.输入1-7的数字,输出对应的“星期几”

s = "星期一星期二星期三星期四星期五星期六星期日"
n =int(input("请输入数字(1-7):"))
if (0 < n <8):
print(s[((n-1)*3):(n*3)])

c.识别身份证号中的省市区、年龄、性别

mport time
provinces = {
11:'北京市',
12:'天津市',
13:'河北省',
14:'山西省',
15:'内蒙古自治区',
21:'辽宁省',
22:'吉林省',
23:'黑龙江省',
31:'上海市',
32:'江苏省',
33:'浙江省',
34:'安徽省',
35:'福建省',
36:'江西省',
37:'山东省',
41:'河南省',
42:'湖北省',
43:'湖南省',
44:'广东省',
45:'广西壮族自治区',
46:'海南省',
50:'重庆市',
51:'四川省',
52:'贵州省',
53:'云南省',
54:'西藏自治区',
61:'陕西省',
62:'甘肃省',
63:'青海省',
64:'宁夏回族自治区',
65:'新疆维吾尔自治区',
71:'台湾省',
81:'香港特别行政区',
91:'澳门特别行政区'
}
def decide(cardID):
province=cardID[0:2]
birthdayYear=cardID[6:10]
localYear=time.strftime('%Y')
age=int(localYear)-int(birthdayYear)
sex=cardID[16:17]
print("省份为:", provinces.get(int(province)))
print("年龄为:{}".format(age))
if int(sex)%2==0:
print("性别:女")
else:
print("性别,男")
cardID=input("请输入身份证号:")
decide(cardID)

d.用字符串操作生成python文档各库的网址(起始网址在这里https://docs.python.org/3.6/library/index.html)

a = "https://docs.python.org/3.6/library/"
b = ".html"
c = input("请输入库名:")
print("网址为:%s%s%s"%(a,c,b))

 

posted @ 2017-09-14 21:03  丁镜钿  阅读(790)  评论(0编辑  收藏  举报