在Python中,有办法让乌龟执行我创建的随机函数吗?

huangapple go评论64阅读模式
英文:

Is there a way to make the turtle in python run a random function that I create?

问题

我从随机模块中导入了choice函数,然后尝试将我定义的函数放入一个列表中,代码如下:

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

我期望它能随机选择一个函数来执行,但实际上它从列表中的第一个函数开始执行,然后在清空屏幕后继续执行列表中的下一个函数,直到运行完所有函数。

英文:

I imported choice from random and tried putting the functions I defined in a list, and I typed in:

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

I expected it to choose a random one of the functions, but it just starts with the first function, and then once it clears the screen, goes on to the next function in the list until its run them all.

答案1

得分: 2

只返回翻译好的部分:

通过编写[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([...])()
                   ^
                   |
                   |
                  这里
英文:

By writing [SonicTheHedgehogDrawing(), turtle.KnucklesTheEchidnaDrawing(), turtle.MilesTailsProwlerDrawing(), turtle.BeanTheDynamiteDrawing(), turtle.AmyTheHedgehogDrawing(), turtle.EggmanDrawing(), turtle.RougeTheBatDrawing(), turtle.BlazeTheCatDrawing()] you create a list of the results of all the function calls. To collect the functions in a list before evaluating them, omit the braces (), like so: [SonicTheHedgehogDrawing, turtle.KnucklesTheEchidnaDrawing, turtle.MilesTailsProwlerDrawing, turtle.BeanTheDynamiteDrawing, turtle.AmyTheHedgehogDrawing, turtle.EggmanDrawing, turtle.RougeTheBatDrawing, turtle.BlazeTheCatDrawing]. But then you have to call the result of choice once:

print(choice([...])())
                   ^
                   |
                   |
                  here

huangapple
  • 本文由 发表于 2023年2月17日 23:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定