Python case 2 - turtle library to draw pentagram V_3.0

Hello everyone, I'm cabbage carrot balls

Life is too short, Python is a song

Achieve 100 million technicians
Draw a five-pointed star


project import

Version 3.0: The drawing of the five-pointed star is encapsulated into a function, thereby making the program more modular

Project analysis - clarifying ideas

  1. Analyze the problem: The computational part of the analysis problem
  2. Defining the problem: dividing the input, processing, and output parts of the problem (IPO)
  3. Designing Algorithms: The Heart of the Computational Section

flow chart


Knowledge point learning – from the ground up


function definition

def <function name> (<parameter list>):

    <函数体>

    return <返回值列表>

function call process

  • The calling program suspends execution at the calling function

  • Assign parameter (actual parameter) to parameter (formal parameter) when calling

  • execute function body

  • Return the execution result, return to the calling place to continue execution

Hands-on programming – hands-on, let the program run

reference code


"""
作者:白菜胡萝丸子
版本3.0:将五角星的绘制封装到函数里,进而使程序更加模块化
"""
import turtle


def pentagram(side_length):
    i = 0
    while i < 5:
        turtle.forward(side_length)
        turtle.right(144)
        i = i + 1


def main():
    turtle.speed(10)
    side = 100
    pentagram(side)

    turtle.exitonclick()


if __name__ == "__main__":
    main()

Results of the


Draw a five-pointed star


Take it to the next level - let the program evolve a little bit

I want to draw more repeated five-pointed stars, change the color of the edges of the five-pointed star, and fill the five-pointed star with color. How to optimize the program?

Guess you like

Origin blog.csdn.net/coco264/article/details/123566719