【问题标题】:How can I include this as an argument?我怎样才能把它作为一个论点?
【发布时间】:2023-03-13 16:57:02
【问题描述】:

我正在尝试绘制南瓜灯,但我不知道如何应用我定义的这些参数...有什么函数可以让我绘制我创建的随机形状?

from turtle import*

pumpkin_size=input("Would you like your pumpkin to be tall, fat, or small? ")
turtle_1=Turtle()
turtle_2=Turtle()


def tall_pumpkin():
    color("orange")
    shape("circle")
    shapesize(20,16,5)
    fillcolor("orange")

def fat_pumpkin():
    color("orange")
    shape("circle")
    shapesize(15,18,5)

def small_pumpkin():
    color("orange")
    shape("circle")
    shapesize(14,14,5)

interpret_size = {
    "fat": "fat_pumpkin",
    "tall": "tall_pumpkin",
    "small": "small_pumpkin",
    }


exitonclick()

【问题讨论】:

    标签: python python-3.x turtle-graphics


    【解决方案1】:

    一些修复将解决该问题:

    from turtle import *
    
    canvas = Screen()
    canvas.setup(400,200)
    
    pumpkin_size = raw_input("Would you like your pumpkin to be tall, fat, or small? ")
    turtle = Turtle()
    
    print 'selection {}'.format(pumpkin_size)
    
    
    def tall_pumpkin():
        turtle.color("orange")
        turtle.shape("circle")
        turtle.shapesize(20,16,5)
        turtle.fillcolor("orange")
    
    
    def fat_pumpkin():
        turtle.color("orange")
        turtle.shape("circle")
        turtle.shapesize(15,18,5)
    
    
    def small_pumpkin():
        turtle.color("orange")
        turtle.shape("circle")
        turtle.shapesize(14,14,5)
    
    interpret_size = {
        "fat": fat_pumpkin,
        "tall": tall_pumpkin,
        "small": small_pumpkin,
        }
    
    interpret_size[pumpkin_size]()
    canvas.exitonclick()
    

    首先您需要声明一个画布对象,然后使用"fat": fat_pumpkin 而不是"fat": "fat_pumpkin" 现在您可以调用interpret_size[pumpkin_size](),它会从输入键调用函数。

    另一件事是使用您的turtle 实例并设置它自己的属性,例如colorshape

    • python3 用户input()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 2021-12-13
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多