【问题标题】:Is there a way to make the turtle in python run a random function that I create?有没有办法让 python 中的海龟运行我创建的随机函数?
【发布时间】:2023-02-17 23:43:19
【问题描述】:

我从随机导入选择并尝试将我定义的函数放在列表中,然后输入:

print(choice([SonicTheHedgehogDrawing(), 
              turtle.KnucklesTheEchidnaDrawing(), 
              turtle.MilesTailsProwlerDrawing(), 
              turtle.BeanTheDynamiteDrawing(), 
              turtle.AmyTheHedgehogDrawing(), 
              turtle.EggmanDrawing(),
              turtle.RougeTheBatDrawing(), 
              turtle.BlazeTheCatDrawing()]))

我预计它会随机选择一个功能,但它只是从第一个功能开始,然后一旦它清除屏幕,就会继续执行列表中的下一个功能,直到它运行所有功能。

【问题讨论】:

  • 您正在调用每个函数。删除()然后调用choice返回的函数。

标签: python function python-turtle p-lang


【解决方案1】:

通过编写[SonicTheHedgehogDrawing(), turtle.KnucklesTheEchidnaDrawing(), turtle.MilesTailsProwlerDrawing(), turtle.BeanTheDynamiteDrawing(), turtle.AmyTheHedgehogDrawing(), turtle.EggmanDrawing(), turtle.RougeTheBatDrawing(), turtle.BlazeTheCatDrawing()],您可以创建所有函数调用结果的列表。要在评估之前将函数收集到列表中,请省略大括号 (),例如:[SonicTheHedgehogDrawing, turtle.KnucklesTheEchidnaDrawing, turtle.MilesTailsProwlerDrawing, turtle.BeanTheDynamiteDrawing, turtle.AmyTheHedgehogDrawing, turtle.EggmanDrawing, turtle.RougeTheBatDrawing, turtle.BlazeTheCatDrawing]。但是你必须调用一次choice的结果:print(choice([...])())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2022-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多