#画个二叉树
import turtle
def draw_brach(brach_length):
if brach_length>5:
if brach_length<40:
turtle.color('green')
else:
turtle.color('red')
turtle.forward(brach_length)
print('向前',brach_length)
turtle.right(25)
print('右转25')
draw_brach(brach_length-15)
turtle.left(50)
print('左转50')
draw_brach(brach_length-15)
if brach_length<40:
turtle.color('green')
else:
turtle.color('red')
turtle.right(25)
print('右转25')
turtle.backward(brach_length)
print('返回',brach_length)
def main():
turtle.left(90)
turtle.penup()
turtle.backward(150)
turtle.pendown()
turtle.color('red')
draw_brach(110)
turtle.exitonclick()
if __name__=='__main__':
main()
#总结
"""
import turtle
turtle.forward()
turtle.backward()
turtle.right()
turtle.left()
turtle.pendown()
turtle.goto()
turtle.penup()
turtle.circle()
setx() #移x
sety() #移y
setheading(angle) #朝向
home()
dol(r) #圆点
turtle.fillcolor()
turtle.color(pencolor,fillcolr)
turtle.filling()
turtle.begin_fill()
turtle.end_fill()
turtle.hideturtle()
turtle.showturtle()
turtle.mainloop()或turtle.done()
turtle.mode() #模式
turtle.delay() #延时
turtle.begin_poly() #顶点
turtle.end_poly()
"""
#随机random模块
'''
random() #0~1
uniform(1,3) #随机浮点数
randint(1,3) #随机整数数
randrang(1,4,2) #
choice([1,2,3]) #从序列中随机读取
choices(seq,3) #从序列中随机读取3个
shuffle(X) #打乱
sample(seq,3) #随机取3个
'''
#发红包
import random
morey=100
remains=0
i=0
while morey>=0:
i=i+1
a=random.randint(1,15)
morey=morey-a
if morey>=0:
print('第'+str(i)+'个人收到'+str(a)+'元,'+'剩余'+str(morey)+'元')
remains=morey
else:
break
if remains>0:
print('第'+str(i)+'个人收到'+str(remains)+'元,'+'剩余0元')
#随机验证码
#import string
#ascii_lowercase 小写
#ascii_uppercase 大写
#ascii_letter 所有大小写字母
#digits 0~9
import random
import string
s=string.digits + string.ascii_letters
v=random.sample(s,4)
print(v)
print(' '.join(v))
#随机图
import turtle as tt
from random import randint
tt.TurtleScreen.__RUNNING=True
tt.speed(0)
tt.bgcolor('black')
tt.setpos(-25,25) #改变初始位置
tt.colormode(255)
cnt=0
while cnt<2000:
r=randint(0,255)
g=randint(0,255)
b=randint(0,225)
tt.pencolor(r,g,b)
tt.forward(50+cnt)
tt.right(91)
cnt +=1
tt.done()
#日期模块datetime
#datetime模块中的类
#date
#time
#datetime
#imedelta 表时间间隔
#datetime_CAPI C语言接口
#tzinfo 与时区有关的信息
#datetime模块中的常量
#MAXYEAR 最大年份9999
#MINYEAR 最小年份1
import datetime
day=datetime.datetime.now()
day2=datetime.date.today()
print("当前日期:",day)
print("年 月 日",day2)
#datetime.datetime.now()
#datetime.utcnow() #UCT表示
#datetime.now()
#绘制数码管显示日期
import turtle as t
import datetime
def drawGap(): #一个管
t.penup()
t.fd(5)
def drawLine(draw): #画吗?
drawGap()
t.pendown()if draw else t.penup()
t.fd(40)
drawGap()
t.right(90)
def drawDigit(d):
drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,2,6,8] else drawLine(False)
t.left(90)
drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
t.left(180)
t.penup()
t.fd(20)
def drawDate(date):
t.pencolor('red')
for i in date:
if i=='-':
t.write("年",font=("Arial",18,"normal"))
t.pencolor('green')
t.fd(40)
elif i=='=':
t.write("月",font=("Arial",18,"normal"))
t.pencolor('blue')
t.fd(40)
elif i=='+':
t.write("日",font=("Arial",18,"normal"))
else:
drawDigit(eval(i))
def main():
t.setup(800,350,200,200)
t.penup()
t.fd(-350)
t.pensize(5)
drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
t.hideturtle()
t.exitonclick() #点一下退出
main()