失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 圣诞节!教你用 Python 画棵圣诞树

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

时间:2020-03-12 00:44:30

相关推荐

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

作者 |糖甜甜甜

责编 | 胡巍巍

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

最简单:

1height=5

2

3stars=1

4foriinrange(height):

5print((''*(height-i))+('*'*stars))

6stars+=2

7print((''*height)+'|')

效果:

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

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

方法一:

1importturtle

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

17foriinrange(1,17):

18y=30*i

19forjinrange(i-k):

20x=30*j

21square.goto(x,-y+280)

22square.stamp()

23square.goto(-x,-y+280)

24square.stamp()

25ifi%4==0:

26x=30*(j+1)

27circle.color('red')

28circle.goto(-x,-y+280)

29circle.stamp()

30circle.goto(x,-y+280)

31circle.stamp()

32k+=2

33ifi%4==3:

34x=30*(j+1)

35circle.color('yellow')

36circle.goto(-x,-y+280)

37circle.stamp()

38circle.goto(x,-y+280)

39circle.stamp()

40square.color('brown')

41foriinrange(17,20):

42y=30*i

43forjinrange(3):

44x=30*j

45square.goto(x,-y+280)

46square.stamp()

47square.goto(-x,-y+280)

48square.stamp()

49turtle.exitonclick()

效果:

方法二:

1fromturtleimport*

2importrandom

3importtime

4

5n=80.0

6

7speed("fastest")

8screensize(bg='seashell')

9left(90)

10forward(3*n)

11color("orange","yellow")

12begin_fill()

13left(126)

14

15foriinrange(5):

16forward(n/5)

17right(144)

18forward(n/5)

19left(72)

20end_fill()

21right(126)

22

23color("darkgreen")

24backward(n*4.8)

25deftree(d,s):

26ifd<=0:return

27forward(s)

28tree(d-1,s*.8)

29right(120)

30tree(d-3,s*.5)

31right(120)

32tree(d-3,s*.5)

33right(120)

34backward(s)

35tree(15,n)

36backward(n/2)

37

38foriinrange(200):

39a=200-400*random.random()

40b=10-20*random.random()

41up()

42forward(b)

43left(90)

44forward(a)

45down()

46ifrandom.randint(0,1)==0:

47color('tomato')

48else:

49color('wheat')

50circle(2)

51up()

52backward(a)

53right(90)

54backward(b)

55time.sleep(60)

效果:

感兴趣的话就赶紧尝试一下吧!

祝大家平安夜快乐!!!圣诞节快乐!!!

作者:糖甜甜甜,985高校经管研二,擅长用Python、R、tableau等工具结合统计学和机器学习模型做数据分析。个人公众号:经管人学数据分析(ID:DAT-)

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

热 文推 荐

☞程序员相亲实录:有人被小三,有人重逢初一同学

☞更新微信 7.0,你后悔了吗?

☞刘强东无罪!

☞去哪儿网裁员超百人;快播王欣晒新团队合照;腾讯联想推定制浏览器 | 极客头条

☞猜对今年ICO大溃败与BCH算力战,小扎铁哥们能猜对明年的房地产区块链吗?

☞减少代码中该死的 if else 嵌套

☞印度小哥“神剑”:PDF提取表格so easy!

☞霸气!女程媛征男友的需求说明书!

print_r('点个好看吧!');

var_dump('点个好看吧!');

NSLog(@"点个好看吧!");

System.out.println("点个好看吧!");

console.log("点个好看吧!");

print("点个好看吧!");

printf("点个好看吧!\n");

cout<<"点个好看吧!"<<endl;

Console.WriteLine("点个好看吧!");

fmt.Println("点个好看吧!");

Response.Write("点个好看吧!");

alert("点个好看吧!")

echo "点个好看吧!"

点击“阅读原文”,打开 CSDN App 阅读更贴心!

喜欢就点击“好看”吧

如果觉得《圣诞节!教你用 Python 画棵圣诞树》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。