【python】把“名字螺旋线”代码中的 绘制部分 用函数重写并在main中调用。

题目:改造“家庭成员名字螺旋线“的代码,把其中绘制部分使用函数的方式重写,并在 main 函数中进行调用。注意:判断好函数需要得到的参数,并正确书写函数参数信息。
背景:本人刚刚初学一点C++,看了同事的python交互程序感觉很有趣心血来潮借了他的学习资料来学习,虽然会写c++的函数但是到python就不会写了。自己搜了教程感觉糊里糊涂,本来想直接def再复制粘贴后来发现原程序连main都没有,即使写出来了那这个函数的变量又是什么呢?(总而言之就是,python里怎么把主函数的一部分改成自定义函数再在main函数中调用的问题)……求解答。感激不尽。
问题相关代码:
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
colors = [ "red", "yellow", "blue", "green", "orange",
           "purple","white", "brown ", "gray", "pink"  ]
family =[] # Set up an empty list for family names
# Ask for the first name
name = turtle.textinput("My family",
                        "Enter a name,or just hit [ENTER] to end: ")
# Keep asking for names
while name != "":
    #Add their name to the family list
    family.append(name)
    #Ask for another name,or end
    name = turtle.textinput("My family",
                        "Enter a name,or just hit [ENTER] to end: ")
for x in range(100):
    t.pencolor(colors[x%len(family)]) # Rotate through the colors
    t.penup()#Don't draw the regular spiral lines
    t.forward(x*4)
    # Just move the turtle on the screen
    t.pendown()
    # Draw the next family member's name
    t.write(family[x%len(family)],font = ("Arial", int((x+4)/4),"bold") )
    t.left(360/len(family) +2)#Turn left for our spiral

没什么好大的改动,如下:

import turtle

def Draw_Word(t, familyt, n):
    colors = ["red", "yellow", "blue", "green", "orange",
               "purple", "white", "brown ", "gray", "pink"]
    for x in range(n):
        t.pencolor(colors[x % len(familyt)])  # Rotate through the colors
        t.penup()#Don't draw the regular spiral lines
        t.forward(x*4)
        # Just move the turtle on the screen
        t.pendown()
        # Draw the next family member's name
        t.write(familyt[x % len(familyt)], font = ("Arial", int((x + 4) / 4), "bold"))
        t.left(360 / len(familyt) + 2)  # Turn left for our spiral

t = turtle.Pen()
turtle.bgcolor("black")

family = []

while True:
    name = turtle.textinput("My family",
                        "Enter a name,or just hit [ENTER] to end: ")
    if name == "":
        break
    family.append(name)

if family:
    Draw_Word(t, family, 100)

python可以不需要写main函数,你的代码如果没有报错就能直接运行的