甜兔宇宙 2021-12-28 23:52 采纳率: 50%
浏览 58
已结题

【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

  • 写回答

2条回答 默认 最新

  • chuifengde 2021-12-29 10:23
    关注

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

    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)
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 请回答用户的提问 8月21日
  • 系统已结题 1月6日
  • 已采纳回答 12月29日
  • 创建了问题 12月28日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度