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

1.用循环画五角星

from turtle import *
fillcolor("yellow")
begin_fill()
while True:
    forward(200)
    right(144)
    if abs(pos())<1:
        break
end_fill()

 

 

2. 用循环画同心圆

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

 

3.用while循环画太阳花

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

 

 

 

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

import turtle
#画长方形
def Rectangle(x, y, width, height):
        #设置画笔
        turtle.penup()
        turtle.color("red", "red")
        turtle.goto(x, y)
        turtle.pendown()
        #开始画
        turtle.begin_fill()
        for i in range(2):
                turtle.forward(width)
                turtle.right(90)
                turtle.forward(height)
                turtle.right(90)
        turtle.end_fill()
#画五角星
def Star(radius):
        #设置画笔
        turtle.penup()
        turtle.color("yellow", "yellow")
        turtle.pendown()
        #开始画
        #确定五个顶点的位置坐标
        turtle.penup()
        p1 = turtle.position()
        turtle.circle(-radius, 72)
        p2 = turtle.position()
        turtle.circle(-radius, 72)
        p3 = turtle.position()
        turtle.circle(-radius, 72)
        p4 = turtle.position()
        turtle.circle(-radius, 72)
        p5 = turtle.position()
        turtle.circle(-radius, 72)
        turtle.pendown()
        #连接五个顶点并且填充为黄色
        turtle.begin_fill()
        turtle.goto(p3)
        turtle.goto(p5)
        turtle.goto(p2)
        turtle.goto(p4)
        turtle.goto(p1)
        turtle.end_fill()
#画大五角星
def BigStar(Bigcenter_x,Bigcenter_y, radius):
        #设置初始位置
        turtle.penup()
        turtle.goto(Bigcenter_x, Bigcenter_y + radius)
        turtle.pendown()
        #开始画大五角星
        Star(radius)
def SmallStar(center_x, center_y, radius):
        #设置初始位置
        turtle.penup()
        turtle.home()
        turtle.goto(center_x, center_y)
        angle = turtle.towards(Bigcenter_x, Bigcenter_y) - 90
        turtle.goto(center_x, center_y + radius)
        turtle.circle(-radius, -angle)
        turtle.pendown()
        #开始画小五角星
        Star(radius)
'''
Rectangle(600, 400)
Bigcenter_x = 100
Bigcenter_y = -100
BigStar(100, -100, 60)
SmallStar(200, -40, 20)
SmallStar(240, -80, 20)
SmallStar(240, -140, 20)
SmallStar(200, -180, 20)
'''
#定义尺寸和左上角位置坐标
#左上角位置坐标
x = -400
y = 200
#国旗宽度
width = 800
height =width * 2 / 3
per = width / 30
Bigcenter_x = x + per * 5
Bigcenter_y = y - per * 5
Bigradius = per * 3
center_x = [x + per * 10,x + per * 12, x + per * 12, x + per * 10]
center_y = [y - per * 2, y - per * 4, y - per * 7, y - per * 9]
Smallradius = per
#开始画
turtle.hideturtle()
Rectangle(x, y, width, height)
BigStar(Bigcenter_x, Bigcenter_y, Bigradius)
for i in range(4):
        SmallStar(center_x[i], center_y[i], Smallradius)

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

import turtle

def draw_diamond(brad):
    brad.forward(90)
    brad.right(45)
    brad.forward(90)
    brad.right(135)

def draw_art():
    
    window=turtle.Screen()
    window.bgcolor("yellow")
    
    brad=turtle.Turtle()
    brad.shape("turtle")
    brad.color("red")
    brad.speed("fastest")


    for i in range(1,37):
        draw_diamond(brad)
        draw_diamond(brad)
        brad.left(10)

    brad.right(90)
    brad.forward(155)
    brad.color('blue')
    brad.forward(145)

    window.exitonclick()

draw_art()

 

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

def decide(ID):
    if len(ID)<12:
        print("你输入的学号有误,请正确输入!")
    elif ID.isdigit() !=True:
        print("你输入的学号有误,请正确输入!")
    else:
        grade=ID[0:4]
        profession=ID[4:8]
        order=ID[10:12]
        print("{}级".format(grade),end="")
        if profession=='0611':
            print("网络工程",end="")
        print("{}号同学".format(order))
ID=input("请输入学号:")
decide(ID)

 

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

def weekday(week):
    week=int(week)
    if 0<week<8:
        i=week-1
        str=['星期一','星期二','星期三','星期四','星期五','星期六','星期日']
        print("数字{}是{}".format(week,str[i]))
    else:
        print("输入有误")
week=input("输入1~7内的数字:")
weekday(week)

 

 

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

ID=input('请输入18位身份证号码: ')
if len(ID)==18:
  print("你的身份证号码是 "+ID)
else:
  print("错误,请重新输入")
  
ID_add=ID[0:6]
ID_birth=ID[6:10]
ID_sex=ID[14:17]

if int(ID_add)==440882:
  print("省市区:广东省湛江市雷州市")
elif int(ID_add)==440883:
    print("省市区:广东省湛江市霞山区")
elif int(ID_add)==440884:
    print("省市区:广东省湛江市坡头区")
elif int(ID_add)==440885:
    print("省市区:广东省湛江市赤坎区")
elif int(ID_add)==440886:
    print("省市区:广东省湛江市廉江市")
elif int(ID_add)==440887:
    print("省市区:广东省湛江市吴川市")

    
birth=2017-int(ID_birth[0:4])
print("年龄:{}".format(birth)) 
  
if int(ID_sex)%2==0:
  print('性别:女')
else:
  print('性别:男')
  

 

posted on 2017-09-14 11:15  09陈雨雨  阅读(236)  评论(0编辑  收藏  举报