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

1、用循环画五角星

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

  

2、用循环画同心圆

import turtle
for i in range(6):
    turtle.up()
    turtle.goto(0,-25*i)
    turtle.down()
    turtle.circle(25*i)

3、用while循环画太阳花

 

from turtle import *
color('pink')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos())<1:
        break
end_fill()
done()

4、用函数定义画五个五角星

import turtle
turtle.begin_fill()
turtle.up()
turtle.goto(-600,220) 
turtle.down()
for i in range(5):
    turtle.forward(150)
    turtle.right(144)
turtle.end_fill()

turtle.begin_fill()
turtle.up()
turtle.goto(-400,295)
turtle.setheading(305)
turtle.down()    
for i in range(5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()

turtle.begin_fill()
turtle.up()
turtle.goto(-350,212)
turtle.setheading(30)
turtle.down()
for i in range(5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()

turtle.begin_fill()
turtle.up()
turtle.goto(-350,145)
turtle.setheading(5)
turtle.down()
for i in range(5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()

turtle.begin_fill()
turtle.up()
turtle.goto(-400,90)
turtle.setheading(300)
turtle.down()
for i in range(5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()

5、用函数定义画钻石花瓣的太阳花

import turtle
turtle.speed(30)
turtle.color("yellow")
turtle.fillcolor("pink")
def draw_rhombus(size):
    for i in range(2):
        turtle.fd(size)
        turtle.left(20)
        turtle.fd(size)
        turtle.left(160)
turtle.right(90)
turtle.begin_fill()
for i in range(36):
    draw_rhombus(100)
    turtle.left(10)
turtle.end_fill()
turtle.fd(400)

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

s= input("请输入学号:")
print("您的年级:{}级".format(s[0:4]))
if(int(s[8:10])==43):
    print("专业名称:网络工程专业(一班)")
elif(int(s[8:10])==44):
    print("专业名称:网络工程专业(二班)")
print("班号为:{}".format(s[-2:]))

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

day="星期一星期二星期三星期四星期五星期六星期日"
a=eval(input("请输入星期数字(1-7):"))

pos=(a-1)*3

print(day[pos:pos+3])

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

import 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 check(ID):
    province=ID[0:2]
    birthdayYear=ID[6:10]
    localYear=time.strftime('%Y')
    age=int(localYear)-int(birthdayYear)
    sex=ID[16:17]
    print("省份为:", provinces.get(int(province)))
    print("年龄为:{}".format(age))
    if int(sex)%2==0:
        print("性别:女")
    else:
        print("性别:男")
ID=input("请输入身份证号:")
check(ID)

9、用字符串操作生成python文档各库的网址

a="https://docs.python.org/3.6/library/index"
b=".html"
address=a+b
print(address)

 

posted @ 2017-09-14 13:46  少钏_leo  阅读(390)  评论(0编辑  收藏  举报