Python - 递归函数用海龟画花

Python - drawing a flower with turtle by recursive function

我想画一朵有 num 花瓣的乌龟图形花。但是,当我 运行 我的代码时,我只打印出一个花瓣。我得到的错误在代码的 def flower(num, i = 1) 部分,但我不确定如何修复它。

import time
from turtle import *

pensize(2)
pencolor("orange")
bgcolor("green")
fillcolor("blue")
hideturtle()

def halfPetal():
    forward(50)
    left(30)
    forward(75)
    left(30)
    forward(50)
    left(120)

def petal():
    for i in range(2):
        halfPetal()

def flower(num, i=1):
    if i==1:
        begin_fill()
        for i in range(num):
            petal()
            left(360/petal())
        end_fill()

flower(12)
time.sleep(10)

至少一个问题在行:

        left(360/petal())

petal 不是 return 值,因此您试图除以 None。 Python中没有这样的操作,所以你得到一个致命错误。相反,我认为您需要除以要绘制的花瓣数量:

        left(360.0 / num)