We have learned the concept of variables in high school. For example: we make x = 100, then we can launch x*2 = 200
Try this Python code below
1 Import Turtle 2 3 turtle.shape ("turtle")4 x =5 Turtle.forward (x)6 turtle.left (7) , Turtle.forward (+x)8 9 Turtle.exitonclick ()
Run the above code and the turtle will draw the following pattern
x = 100 declares the variable x and assigns it a value of 100, in the familiar middle school math language, is "Make x equals" /span>
In the next code turtle. Forward (x
Similar to turtle. Forward(2*x) is the distance of twice X units for the Turtle to advance, that is, twice times the distance of 100 units, and eventually the turtle will crawl forward 2 x 100 = 200 units of distance (the longer skew in the image pointing to the top right Line)
Prompted
- Python (and most programming languages) uses * to represent mathematical multiplication to avoid confusion with the letter X
Unlike high school math, variables in Python can be used not only to represent numbers, but also to represent a variety of non-digital things. For example, by ipaomi = Turtle You can change Dr. Mi into a turtle, and you can use the Ipaomi variable to control the turtle's drawing.
The following code, drawing the image and just the same, the difference is that we assign a turtle to the variable Ipaomi
1 Import Turtle 2 3 Ipaomi = Turtle 4 ipaomi.shape ("turtle") 5 x = 6ipaomi.forward (x) 7 ipaomi.left (GB) 8 Ipaomi.forward (x) 9 Ipaomi.exitonclick ()
Practice
Try to draw a house (use a variable to do it, try to adjust the value of the variable, draw a different size of the house)
Prompted
- You might need to use the open-root operation, introduce the math module, and then use the Math module's Sqrt method to perform the open-root operation.
- For example, the following code calculates the square root of 5 and assigns the result of the calculation to the variable x
1 Import Math 2 3 x = math.sqrt (5)
"Original link" http://www.ipaomi.com/2017/11/15/python-0 basics-Quick Start-Fun Tutorial-Dr. Mi-Turtle painting-turtle-2-Change/
Python 0 Basics Quick Start Fun tutorial (Dr. Mi Turtle drawing Turtle) 2. Variable