ในโปรแกรมนี้ เราจะวาดรูปทรงต่างๆ โดยใช้ Turtle ห้องสมุด ในไพทอน Turtle เป็นคุณลักษณะของงูหลามเช่นกระดานวาดภาพ ซึ่งช่วยให้คุณสั่งเต่าให้วาดไปทั่ว รูปร่างต่างๆ ที่เราจะวาดคือ สี่เหลี่ยม สี่เหลี่ยม วงกลม และหกเหลี่ยม
อัลกอริทึม
ขั้นตอนที่ 1:ป้อนข้อมูลด้านยาวสำหรับรูปทรงต่างๆ
ขั้นตอนที่ 2:ใช้วิธีต่างๆ ของเต่า เช่น forward() และ left() ในการวาดรูปทรงต่างๆ
โค้ดตัวอย่าง
import turtle
t = turtle.Turtle()
#SQUARE
side = int(input("Length of side: "))
for i in range(4):
t.forward(side)
t.left(90)
#RECTANGLE
side_a = int(input("Length of side a: "))
side_b = int(input("Length of side b: "))
t.forward(side_a)
t.left(90)
t.forward(side_b)
t.left(90)
t.forward(side_a)
t.left(90)
t.forward(side_b)
t.left(90)
#CIRCLE
radius = int(input("Radius of circle: "))
t.circle(radius)
#HEXAGON
for i in range(6):
t.forward(side)
t.left(300) ผลลัพธ์
SQUARE: Length of side: 100RECTANGLE: Length of side a: 100 Length of side b: 20
CIRCLE: Radius of circle: 60
HEXAGON:Length of side: 100
RECTANGLE:
Length of side a: 100
Length of side b: 20
CIRCLE:
Radius of circle: 60
HEXAGON:Length of side: 100
