圣诞节!教你用Python画棵圣诞树

640?wx_fmt=jpeg


作者 | 糖甜甜甜,985高校经管研二,擅长用 Python、R、tableau 等工具结合统计学和机器学习模型做数据分析。

来源 | 经管人学数据分析(ID:DAT-2017)


如何用Python画一个圣诞树呢?

最简单:

 
 

1height = 5
2
3stars = 1
4for i in range(height):
5    print((' ' * (height - i)) + ('*' * stars))
6    stars += 2
7print((' ' * height) + '|')

效果:

640?wx_fmt=png

哈哈哈哈,总有一种骗了大家的感觉。

其实本文是想介绍Turtle库来画圣诞树。

方法一:

 
 

 1import turtle
 2screen = turtle.Screen()
 3screen.setup(800,600)
 4circle = turtle.Turtle()
 5circle.shape('circle')
 6circle.color('red')
 7circle.speed('fastest')
 8circle.up()
 9square = turtle.Turtle()
10square.shape('square')
11square.color('green')
12square.speed('fastest')
13square.up()
14circle.goto(0,280)
15circle.stamp()
16k = 0
17for i in range(117):
18    y = 30*i
19    for j in range(i-k):
20        x = 30*j
21        square.goto(x,-y+280)
22        square.stamp()
23        square.goto(-x,-y+280)
24        square.stamp()
25    if i % 4 == 0:
26        x = 30*(j+1)
27        circle.color('red')
28        circle.goto(-x,-y+280)
29        circle.stamp()
30        circle.goto(x,-y+280)
31        circle.stamp()
32        k += 2
33    if i % 4 == 3:
34        x = 30*(j+1)
35        circle.color('yellow')
36        circle.goto(-x,-y+280)
37        circle.stamp()
38        circle.goto(x,-y+280)
39        circle.stamp()
40square.color('brown')
41for i in range(17,20):
42    y = 30*i
43    for j in range(3):
44        x = 30*j
45        square.goto(x,-y+280)
46        square.stamp()
47        square.goto(-x,-y+280)
48        square.stamp()
49turtle.exitonclick()

效果:

640?wx_fmt=png

方法二:

 
 

 1from turtle import *
 2import random
 3import time
 4
 5n = 80.0
 6
 7speed("fastest")
 8screensize(bg='seashell')
 9left(90)
10forward(3*n)
11color("orange""yellow")
12begin_fill()
13left(126)
14
15for i in range(5):
16    forward(n/5)
17    right(144)
18    forward(n/5)
19    left(72)
20end_fill()
21right(126)
22
23color("dark green")
24backward(n*4.8)
25def tree(d, s):
26    if d <= 0return
27    forward(s)
28    tree(d-1, s*.8)
29    right(120)
30    tree(d-3, s*.5)
31    right(120)
32    tree(d-3, s*.5)
33    right(120)
34    backward(s)
35tree(15, n)
36backward(n/2)
37
38for i in range(200):
39    a = 200 - 400 * random.random()
40    b = 10 - 20 * random.random()
41    up()
42    forward(b)
43    left(90)
44    forward(a)
45    down()
46    if random.randint(01) == 0:
47            color('tomato')
48    else:
49        color('wheat')
50    circle(2)
51    up()
52    backward(a)
53    right(90)
54    backward(b)
55time.sleep(60)

效果:

640?wx_fmt=gif

好了,我要先去画圣诞树啦~ 祝大家圣诞节快乐!!!smiley_66.pngsmiley_66.pngsmiley_66.png

本文为作者投稿,版权归对方所有。


公开课预告

推荐系统


本次分享带你揭开个性化推荐的神秘面纱,从推荐算法到大型系统架构进行全面剖析。


添加小助手微信csdnai2,回复:推荐系统,加入课程交流群,课程回放以及PPT将在群内分享。


640?wx_fmt=jpeg


推荐阅读


640?wx_fmt=png

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值