龟在python中,不想打印线

时间:2016-03-03 02:24:15

标签: python turtle-graphics

我正在编写此代码来打印多边形。如果数字是两个或更少,它应该根据我的教授打印,但我无法弄清楚这一点。有人可以告诉我,我需要在我的函数中写入2个或更少的侧面数字,以便不打印任何内容吗?

import turtle
import math
turtle.shape("turtle")
turtle.speed(3)
from turtle import *
pensize (5)
pencolor ("purple")

def polygon(num_side, length):
    for i in range(num_side):
        turtle.forward(length)
        turtle.left(360/num_side)

print(polygon(2, 100)) #nothing?
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(3,100)) #triangle
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(4, 100)) #square  
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(5, 100)) #pentagon
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(6, 100)) #hexagon
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(7, 100)) #heptagon

1 个答案:

答案 0 :(得分:0)

如果边数小于3,则需要退出,如果n大于2,则只输出。因此,if将处理此问题。

import turtle
import math
turtle.shape("turtle")
turtle.speed(3)
turtle.pensize (5)
turtle.pencolor ("purple")

def polygon(num_side, length):
    if num_side > 2:
        for i in range(num_side):
            turtle.forward(length)
            turtle.left(360/num_side)

turtle.pendown()
for n in range(10):
    polygon(n, 100)